选中复选框添加到datagridview [英] get selected check box added to a datagridview
本文介绍了选中复选框添加到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨,,
在我的Windows窗体中有一个datagridview,我已经使用此代码动态添加了复选框
Hi,,
In my windows form there is a datagridview and i have added checkboxes dynamically using this code
dgResult.AllowUserToAddRows = false;
//Below i create on check box column in the datagrid view
dgResult.Columns.Clear();
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
//set name for the check box column
colCB.Name = "chkcol";
colCB.HeaderText = "";
//If you use header check box then use it
colCB.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgResult.Columns.Add(colCB);
//Select cell where checkbox to be display
Rectangle rect = this.dgResult.GetCellDisplayRectangle(0, -1, true); //0 Column index -1(header row) is row index
//Mention size of the checkbox
chkbox.Size = new Size(18, 18);
//set position of header checkbox where to places
rect.Offset(40, 2);
chkbox.Location = rect.Location;
chkbox.CheckedChanged += chkBoxChange;
//Add CheckBox control to datagridView
this.dgResult.Controls.Add(chkbox);
现在我想获取所选复选框并获取整个行值。 ..有可能,如果有的话,任何人都可以帮助我...
提前致谢
Now i want to get the selected checkbox and get the entire row values ... Is it possible , if so can anyone help me...
Thanks in advance
推荐答案
你好朋友,
试试这样...
hello friend,
Try like this...
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox check1=(CheckBox)GridView1.Rows[i].FindControl("Check1");
if (check1.Checked == true)
{
String rowval1= GridView1.Rows[i].Cells[1].Text;
String rowval2 = GridView1.Rows[i].Cells[2].Text;
}
全部谢谢你的回答...
我这样做了它让我得到了所需要的结果
Thanks All For Your Answers...
I did it this way and it got me the required result
foreach (DataGridViewRow row in dgResult.Rows)
{
if (row.Cells[SelectColumnIndex].Value != null && Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
{
objTempDevice.DeviceName = row.Cells["DeviceName"].Value.ToString();
objTempDevice.BroadworksDeviceId = row.Cells["BroadworksDeviceID"].Value.ToString();
objTempDevice.GroupId = row.Cells["BusinessVoIP"].Value.ToString();
objTempDevice.ServiceProviderId = row.Cells["ServiceProviderId"].Value.ToString();
lstDeviceDetails.Add(objTempDevice);
}
}
这篇关于选中复选框添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文