I have a MetroFramework.Controls.MetroGrid in my windows forms application. In frmPatientList_Shown I call a method loadPatientList(). I also add a DataGridViewLinkColumn after binding the dtb to the gridview. Clicking the link opens a new form where I update Patient data, and on formEditPatient.FormClosed I call the loadPatientList() method again, This time the DataGridViewLinkColumn is being added twice. How can I prevent to Add the link twice ? Here is my code:
private void frmPatientList_Shown(object sender, EventArgs e)
{
loadPatientList();
}
private void loadPatientList()
{
DataTable dtb = Patient.getPatientList();
bindToGrid(dtb);
}
private void bindToGrid(DataTable dtb)
{
dataGridView1.DataSource = null;
using (dtb)
{
dataGridView1.DataSource = dtb;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns[0].Name = "PatientId";
dataGridView1.Columns[0].HeaderText = "ID";
dataGridView1.Columns[0].DataPropertyName = "PatientId";
// more code here.
}
DataGridViewLinkColumn EditLink = new DataGridViewLinkColumn();
EditLink.UseColumnTextForLinkValue = true;
EditLink.HeaderText = " Edit ";
EditLink.DataPropertyName = "lnkColumn";
EditLink.LinkBehavior = LinkBehavior.SystemDefault;
EditLink.Text = "Edit";
dataGridView1.Columns.Add(EditLink);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 8 && e.RowIndex >= 0)
{
using (frmEditPatient formEditPatient = new frmEditPatient(id))
{
formEditPatient.FormClosed += FormEditPatient_FormClosed;
formEditPatient.ShowDialog();
}
}
}
private void FormEditPatient_FormClosed(object sender, FormClosedEventArgs e)
{
loadPatientList();
}
Any help would be appreciated.
In frmPatientList_Shown function you add the link
private void frmPatientList_Shown(object sender, EventArgs e)
{
DataGridViewLinkColumn EditLink = new DataGridViewLinkColumn();
EditLink.UseColumnTextForLinkValue = true;
EditLink.HeaderText = " Edit ";
EditLink.DataPropertyName = "lnkColumn";
EditLink.LinkBehavior = LinkBehavior.SystemDefault;
EditLink.Text = "Edit";
dataGridView1.Columns.Add(EditLink);
loadPatientList();
}
In bindToGrid function you do not add the link
private void bindToGrid(DataTable dtb)
{
dataGridView1.DataSource = null;
using (dtb)
{
dataGridView1.DataSource = dtb;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns[0].Name = "PatientId";
dataGridView1.Columns[0].HeaderText = "ID";
dataGridView1.Columns[0].DataPropertyName = "PatientId";
// more code here.
}
//DataGridViewLinkColumn EditLink = new DataGridViewLinkColumn();
//EditLink.UseColumnTextForLinkValue = true;
//EditLink.HeaderText = " Edit ";
//EditLink.DataPropertyName = "lnkColumn";
//EditLink.LinkBehavior = LinkBehavior.SystemDefault;
//EditLink.Text = "Edit";
//dataGridView1.Columns.Add(EditLink);
}
I hope it will help you.
You check if the column exist,
bool hasEditColumn = false;
foreach (DataGridViewColumn item in dataGridView1.Columns)
{
if (item.GetType() == typeof(DataGridViewLinkColumn) && item.HeaderText == "Edit")
{
hasEditColumn = true;
break;
}
}
if (!hasEditColumn)
{
DataGridViewLinkColumn EditLink = new DataGridViewLinkColumn();
EditLink.UseColumnTextForLinkValue = true;
EditLink.HeaderText = "Edit";
EditLink.DataPropertyName = "lnkColumn";
EditLink.LinkBehavior = LinkBehavior.SystemDefault;
EditLink.Text = "Edit";
dataGridView1.Columns.Add(EditLink);
dataGridView1.Refresh();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.