Wednesday, November 5, 2008

implement cheakbox in datagrid view of windows formc

code for Add in data from one grid to another grid on basis of selected cheakbox

private void addL5()//Add on select cheakbox;
{
int j = dgvMyL5.Rows.Count;
foreach (DataGridViewRow drow in dgvAllL5.Rows)
{

if (drow.Cells["select1"].Value != null)
{
if (Boolean.Parse(drow.Cells["select1"].Value.ToString()) == true)
{
if (j == 0)
{

dgvMyL5.Rows.Add();
dgvMyL5["serialNo2", j].Value = j + 1;
dgvMyL5["myComponentsFixedID", j].Value = drow.Cells["ComponentsFixedID"].Value;
dgvMyL5["componentsName", j].Value = drow.Cells["L5ComponentsName"].Value;
dgvMyL5["factoryPrice", j].Value = 0;
j += 1;

}
else
{
int flag = 0;
foreach (DataGridViewRow drow2 in dgvMyL5.Rows)
{
if (drow.Cells["ComponentsFixedID"].Value.ToString() == drow2.Cells["myComponentsFixedID"].Value.ToString())
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if (flag == 0)
{
dgvMyL5.Rows.Add();
dgvMyL5["serialNo2", j].Value = j + 1;
dgvMyL5["myComponentsFixedID", j].Value = drow.Cells["ComponentsFixedID"].Value;
dgvMyL5["componentsName", j].Value = drow.Cells["L5ComponentsName"].Value;
dgvMyL5["factoryPrice", j].Value = 0;
j += 1;
}
}
}
}

}
}


code for Remove data from one grid to another grid on basis of selected cheakbox


private void removeL4()
{
int length = dgvMyL5.Rows.Count;
for (int i = length - 1; i > -1; i--)
{
if (dgvMyL5.Rows[i].Cells["select2"].Value != null)
{
if (Boolean.Parse(dgvMyL5.Rows[i].Cells["select2"].Value.ToString()) == true)
{

dgvMyL5.Rows.RemoveAt(i);

}
}
}
}

No comments: