Move data to next cell on click when repopulating a grid after update
populate grid view and then on cell click i populate text box on cell
click event like,
private void dgvCompany_CellClick(object sender,
DataGridViewCellEventArgs e)
{
int i = dgvCompany.SelectedCells[0].RowIndex;
ID = dgvCompany.Rows[i].Cells[0].Value.ToString();
txtCompanyName.Text =
dgvCompany.Rows[i].Cells[1].Value.ToString();
txtContactName.Text =
dgvCompany.Rows[i].Cells[2].Value.ToString();
txtContactPersonPhone.Text =
dgvCompany.Rows[i].Cells[3].Value.ToString();
txtAddress.Text = dgvCompany.Rows[i].Cells[4].Value.ToString();
txtCity.Text = dgvCompany.Rows[i].Cells[5].Value.ToString();
txtPhone.Text = dgvCompany.Rows[i].Cells[6].Value.ToString();
cbIsActive.Checked =
Convert.ToBoolean(dgvCompany.Rows[i].Cells[7].Value);
}
here ID is string variable after populating text box i edit and save
update and then refill grid as previous method like,
public void FillCompanyInfo()
{
DataTable dtCompanyInfo = objFunctions.GetCompanyInfo();
if(dtCompanyInfo.Rows.Count>0)
{
dgvCompany.DataSource = dtCompanyInfo;
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);
}
}
}
here i use if condition counting column to 8 because each time it add
column Select and after populating grid again when i click on grid and
cell click event fire result in error ,
on line ,
ID = dgvCompany.Rows[i].Cells[0].Value.ToString();
object reference not found because it is getting null on cell 0 while it
was working fine before update on cell click and when i use break point i
am getting ID on cells[1] and cell[0] is null
i am not understanding this error
Hopes for your suggestion
Thanks
No comments:
Post a Comment