如何从datagridview项中获取我选中的复选框项 [英] How To Get my selected checkbox item from datagridview item
问题描述
如何从datagridview项中获取我选中的复选框项。并放入一个数据集(网格中的所有选定项目应存在于数据集中)
How To Get my selected checkbox item from datagridview item. And Placed into One dataset (All Selected items in the Grid Should be present in the dataset )
推荐答案
以下应该有所帮助:如何在Winforms DataGridView中获取CheckBox以响应第一次点击 [ ^ ]
Something例如:
Following should help: How to get a CheckBox in a Winforms DataGridView to react to the first click[^]
Something like:
private void EmployeesGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
bool selected = (bool)_gvEmployees[e.ColumnIndex, e.RowIndex].Value;
_gvEmployees.Rows[e.RowIndex].DefaultCellStyle.BackColor = selected ? Color.Yellow : Color.White;
}
}
如果您想要查找一般的选定项目,请循环浏览所有行并检查每行的复选框值,根据选择,使用你的逻辑。
If you want to find selected items in general, then loop through all rows and check checkbox value for each row and depending on the selection, use your logic.
创建数据集和数据表添加列
Create Dataset and datatable Add the columns
public Form1()
{
InitializeComponent();
GINdata();
}
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public DataTable GINdata()
{
dt.Columns.Add(new DataColumn("vname", typeof(System.String)));
dt.Columns.Add(new DataColumn("Rate", typeof(System.String)));
return dt;
}
添加一个按钮
in btn点击if if复选框放在单元格[2]然后写下这段代码
for(int i = dataGridView1.Rows.Count - 1 ; i> = 0; i--)
{
if((bool)dataGridView1.Rows [i] .Cells [2] .FormattedValue)
{
DataRow dr = dt.NewRow();
dr [ vname] = dataGridView1.Rows [i] .Cells [vname]。值;
dr [Rate] = dataGridView1.Rows [i] .Cells [Rate]。值;
dt.Rows.Add(dr);
}
------- -------------------------
将数据表添加到数据集中
ds.Tables.Add(dt);
string XML = ds.GetXml();
MessageBox.Show(XML);
Add One button
in btn Click if check box is placed in cells[2] then write this code
for (int i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
if ((bool)dataGridView1.Rows[i].Cells[2].FormattedValue)
{
DataRow dr = dt.NewRow();
dr["vname"] = dataGridView1.Rows[i].Cells["vname"].Value;
dr["Rate"] = dataGridView1.Rows[i].Cells["Rate"].Value;
dt.Rows.Add(dr);
}
--------------------------------
Add the datatable into dataset
ds.Tables.Add(dt);
string XML = ds.GetXml();
MessageBox.Show(XML);
这篇关于如何从datagridview项中获取我选中的复选框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!