我正在尝试删除网格视图数据,因为我正在填充网格,
public void FillCompanyInfo()
{
DataTable dtCompanyInfo = new DataTable();
dtCompanyInfo = objFunctions.GetCompanyInfo();
if(dtCompanyInfo.Rows.Count>0)
{
dgvCompany.DataSource = dtCompanyInfo;
}
if (dtCompanyInfo.Rows.Count > 0)
{
if (this.dgvCompany.Columns.Count == 8)
{
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "";
checkColumn.HeaderText = "Select";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values\\
dgvCompany.Columns.Add(checkColumn);
}
}
}
填充网格后,我单击网格行并单击删除按钮,
private void btn_Delete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow r in dgvCompany.Rows)
{
if (Convert.ToBoolean(r.Cells[8].Value)) //cells[4] CONTAINS CHECKBOX COLUMN
{
string strId = r.Cells[1].Value.ToString(); //cells[0] CONTAINS EMPIDCOLUMN
objFunctions.DeleteCompany(strId);
}
}
FillCompanyInfo();
}
但是它在我的网格视图中错误地删除了数据我得到了从 0 到 6 的数字和字符串字段以及第 7 位的 chk 框,而在将数据绑定到网格后我还在第 8 位附加了一列可能是由于某些原因?
在调试时我突然注意到在这个地方,
foreach (DataGridViewRow r in dgvCompany.Rows)
该网格视图以这种方式在 0 位置有 chk 框,然后在 7 个字段,然后在 8 位置有 chk 框如何更改顺序?
希望您的建议提前谢谢