如何遍历datagrid并获取已检查行的值 [英] How to loop through datagrid and get values for rows that are checked

查看:132
本文介绍了如何遍历datagrid并获取已检查行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello House,



请问有没有人知道如何遍历包含复选框列的datagrid,并获取已检查行的值?



我可以使用gridview控件使用以下代码在网络表单中执行此操作:



  int  rowCount =  0 ; 
foreach (DataGridItem DemoGridItem in DemoGrid.Items)
{
CheckBox myCheckbox =(CheckBox)DemoGridItem.Cells [ 0 ]。控件[ 1 ];
if (myCheckbox.Checked == true
{
rowCount时++;

}
}





请问如何在C#Windows应用程序中实现上述代码?在此先感谢。

解决方案

更新:



  int  rowCount =  0 ; 
foreach (DataGridViewRow row in DemoGrid.Rows)
{
DataGridViewCheckBoxCell cell = row.Cells [colCheck] as DataGridViewCheckBoxCell;

if (cell!= null &&!DBNull.Value .Equals(cell.Value)&&( bool )cell.Value == true
{
// 您的代码在这里
}
}


Hello House,

Please does anyone know how to loop through datagrid that has checkbox column, and get values for the rows that are checked?

I can do this in web forms using gridview control with this code:

int rowCount = 0;
            foreach ( DataGridItem DemoGridItem in DemoGrid.Items )
            {
                CheckBox myCheckbox = ( CheckBox ) DemoGridItem.Cells[0].Controls[1];
                if ( myCheckbox.Checked == true )
                {
                    rowCount++;
                    
                }
            }



Please how do i implement the above code in C# Windows application? Thanks in advance.

解决方案

Updated:

int rowCount = 0;
foreach (DataGridViewRow row in DemoGrid.Rows)
{
   DataGridViewCheckBoxCell cell = row.Cells[colCheck] as DataGridViewCheckBoxCell;

   if ( cell!=null && !DBNull.Value.Equals( cell.Value ) && ( bool ) cell.Value == true )
        {
            //Your code here
        }
}


这篇关于如何遍历datagrid并获取已检查行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆