如何在c#中将checkbox列添加到gridview [英] How to add checkbox column to gridview in c#
本文介绍了如何在c#中将checkbox列添加到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我添加了这样的...
I have added like this...
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.AutoSizeMode =
DataGridViewAutoSizeColumnMode.DisplayedCells;
checkColumn.CellTemplate = new DataGridViewCheckBoxCell();
checkColumn.FalseValue = true;
checkColumn.HeaderText = "SelectCompany";
checkColumn.Name = "CheckBoxes";
checkColumn.TrueValue = true;
checkColumn.FalseValue = false;
dgvGetCompanyDetails.Columns.Add(checkColumn);
但我无法勾选复选框
But I am not able to check the checkboxes
推荐答案
获得数据后在网格中,你可以遍历行并检查每个框,如下所示:
Once you have your data in the grid, you can loop through the rows and check each box like this:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[CheckBoxColumn1.Name].Value = true;
}
您可以点击这样的Click事件:
The Click event you can get like this:
private void button1_Click(object sender, EventArgs e)
{
List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
{
rows_with_checked_column.Add(row);
}
}
// Do what you want with the check rows
}
参考:
MSDN上的好例子 [ ^ ]
查看详细说明 [ ^ ]此处。
Refer:
Good example on MSDN[^]
Check out Detailed description[^] here.
Hi,
you can use TemplateFields for the GridView:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Hope this helps:)
这篇关于如何在c#中将checkbox列添加到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文