DataGridViewFilePathColumn
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้public class DataGridViewFilePathColumn : DataGridViewColumn
{
public DataGridViewFilePathColumn()
: base(new DataGridViewFilePathCell())
{
this.DefaultCellStyle.SelectionForeColor = Color.Blue;
this.DefaultCellStyle.ForeColor = Color.Blue;
}
[Category("TOR Setting"), DefaultValue(@"C:\")]
public string Directory { get; set; } = @"C:\";
[Category("TOR Setting"), DefaultValue(true)]
public bool ShowButton { get; set; } = true;
[Category("TOR Setting")]
public Image ImageButton { get; set; } = Properties.Resources.finds;
public override object Clone()
{
var clm = base.Clone() as DataGridViewFilePathColumn;
if (clm != null)
{
//clm.SelectionMode = this.SelectionMode;
clm.Directory = this.Directory;
clm.ShowButton = this.ShowButton;
clm.ImageButton = this.ImageButton;
}
return clm;
}
public override DataGridViewCell CellTemplate
{
get => base.CellTemplate;
set
{
if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewFilePathCell)))
throw new InvalidCastException("Must be a DataGridViewFilePathCell");
base.CellTemplate = value;
}
}
}
public class DataGridViewFilePathCell : DataGridViewLinkCell
{
private static Button sharedBrowseButton;
public DataGridViewFilePathCell() : base()
{
this.LinkColor = Color.Black;
this.ActiveLinkColor = Color.Red;
this.VisitedLinkColor = Color.Blue;
this.LinkBehavior = LinkBehavior.SystemDefault;
if (sharedBrowseButton == null)
{
sharedBrowseButton = new Button
{
FlatStyle = FlatStyle.Flat,
Cursor = Cursors.Hand,
BackColor = Color.Transparent
};
sharedBrowseButton.FlatAppearance.BorderSize = 0;
sharedBrowseButton.FlatAppearance.MouseDownBackColor = Color.Transparent;
sharedBrowseButton.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, Color.Gray);
}
}
// --- จุดที่ 1: ปรับปรุงการจัดการ Event ---
protected override void OnDataGridViewChanged()
{
base.OnDataGridViewChanged();
if (this.DataGridView != null)
{
// ถอดออกก่อนเพื่อความปลอดภัย ไม่ให้เกิดการสมัครซ้ำ
this.DataGridView.ColumnWidthChanged -= DataGridView_LayoutChanged;
this.DataGridView.Scroll -= DataGridView_LayoutChanged;
this.DataGridView.SizeChanged -= DataGridView_LayoutChanged;
this.DataGridView.ColumnWidthChanged += DataGridView_LayoutChanged;
this.DataGridView.Scroll += DataGridView_LayoutChanged;
this.DataGridView.SizeChanged += DataGridView_LayoutChanged;
}
}
// แยก Method ออกมาเพื่อให้ Code สะอาดขึ้น
private void DataGridView_LayoutChanged(object sender, EventArgs e) => RepositionButton();
public void RepositionButton()
{
// เช็คก่อนว่าปุ่มกำลังแสดงผลใน Cell ปัจจุบันหรือไม่
if (sharedBrowseButton == null || !sharedBrowseButton.Visible || this.DataGridView == null) return;
if (this.DataGridView.CurrentCell != this) return;
Rectangle cellRect = this.DataGridView.GetCellDisplayRectangle(this.ColumnIndex, this.RowIndex, false);
// ถ้า Cell ถูก Scroll จนหายไปจากจอ ให้ซ่อนปุ่ม
if (cellRect.Width <= 0 || cellRect.Height <= 0)
{
sharedBrowseButton.Hide();
return;
}
int btnSize = cellRect.Height - 2;
sharedBrowseButton.Size = new Size(btnSize, btnSize);
// วางชิดขวาเสมอ
sharedBrowseButton.Location = new Point(cellRect.Right - btnSize - 1, cellRect.Top + 1);
}
protected override void OnEnter(int rowIndex, bool throughMouseClick)
{
base.OnEnter(rowIndex, throughMouseClick);
var col = (DataGridViewFilePathColumn)this.OwningColumn;
if (rowIndex < 0 || !col.ShowButton) return;
sharedBrowseButton.Image = col.ImageButton;
sharedBrowseButton.Parent = this.DataGridView;
sharedBrowseButton.Show();
sharedBrowseButton.BringToFront();
RepositionButton(); // คำนวณตำแหน่งทันทีที่เข้า
sharedBrowseButton.Click -= BrowseButton_Click;
sharedBrowseButton.Click += BrowseButton_Click;
}
protected override void OnLeave(int rowIndex, bool throughMouseClick)
{
base.OnLeave(rowIndex, throughMouseClick);
sharedBrowseButton?.Hide();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
var col = (DataGridViewFilePathColumn)this.OwningColumn;
var dgv = this.DataGridView;
string baseDir = Path.GetFullPath(col.Directory);
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.InitialDirectory = Directory.Exists(baseDir) ? baseDir : @"C:\";
if (ofd.ShowDialog() != DialogResult.OK)
return;
string sourceFile = Path.GetFullPath(ofd.FileName);
string destFile = sourceFile;
bool insideDirectory =
sourceFile.StartsWith(baseDir, StringComparison.OrdinalIgnoreCase);
// ถ้าอยู่นอก Directory ให้ถามว่าจะ copy เข้าไหม
if (!insideDirectory)
{
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.InitialDirectory = baseDir;
sfd.FileName = Path.GetFileName(sourceFile);
if (sfd.ShowDialog() != DialogResult.OK)
return;
destFile = Path.GetFullPath(sfd.FileName);
try
{
if (!destFile.Equals(sourceFile, StringComparison.OrdinalIgnoreCase))
File.Copy(sourceFile, destFile, true);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
return;
}
}
}
// คำนวณค่าที่จะเก็บใน Cell
string saveValue;
if (destFile.StartsWith(baseDir, StringComparison.OrdinalIgnoreCase))
{
saveValue = FileTor.GetRelativePath(baseDir, destFile);
}
else
{
saveValue = destFile;
}
CommitValue(saveValue);
sharedBrowseButton?.Hide();
dgv?.Focus();
}
}
// เพิ่ม Method นี้ใน DataGridViewFilePathCell
private void CommitValue(object value)
{
this.Value = value;
if (this.DataGridView != null)
{
// แจ้ง Grid ว่าค่าใน Cell เปลี่ยนแปลงแล้ว
this.DataGridView.NotifyCurrentCellDirty(true);
// บังคับให้ Grid รันกระบวนการ EndEdit ตามมาตรฐาน
this.DataGridView.EndEdit();
// ส่งแรงกระเพื่อมไปหา Event ต่างๆ ของ Grid
MethodInfo onCellValueChanged = this.DataGridView.GetType().GetMethod("OnCellValueChanged",
BindingFlags.Instance | BindingFlags.NonPublic);
onCellValueChanged?.Invoke(this.DataGridView, new object[] { new DataGridViewCellEventArgs(this.ColumnIndex, this.RowIndex) });
}
}
protected override void OnContentClick(DataGridViewCellEventArgs e)
{
if (this.RowIndex < 0 || this.Value == null) return;
string cellValue = this.Value.ToString();
string dir = ((DataGridViewFilePathColumn)this.OwningColumn).Directory;
string cleanRelativePath = cellValue.TrimStart('\\');
string fullPath = Path.Combine(dir, cleanRelativePath);
if (File.Exists(fullPath))
{
try { Process.Start(new ProcessStartInfo(fullPath) { UseShellExecute = true }); }
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
else
{
MessageBox.Show("ไม่พบไฟล์ที่ตำแหน่ง:\n" + fullPath, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
SetDefaultCellStyle
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้ public static void SetDefaultCellStyle(
this DataGridView dgv,
int clMan1,string manName1, int cDatetime1, int cDate1, int cTime1,
int clMan2, string manName2, int cDatetime2, int cDate2, int cTime2,
int cDatetimeEdit,List<int> columnCal,
bool SetDetailinRowHeader
)
{
DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.Color.Yellow;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12);
DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
dataGridViewCellStyle2.BackColor = System.Drawing.Color.White;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12);
dgv.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
dgv.RowsDefaultCellStyle = dataGridViewCellStyle2;
});
}
C# DataGridViewLinkCell มองไม่เห็นข้อความ ต้องแก้ยังไง ครับ
DataGridViewFilePathColumn
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
SetDefaultCellStyle
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้