如何在CheckBox中使用DataGridView [英] how can use DataGridView with CheckBox

查看:70
本文介绍了如何在CheckBox中使用DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在我的申请表中进行财务计算。它包含一个3数据网格,它显示3个不同的信用和借记喜欢,现在我的要求是用户希望HighLighted在Datagrid中的一些行,Bcoz突出显示的行一些重要的借记/信用。所以现在我决定对突出显示的行进行复选框选择。当没有必要进行交易时,删除该chkbox中的选择就会清楚地显示行颜色。

i希望你们都明白我的问题。

如何使用gridview with checkbox变色:

解决方案

1。
添加DatagridviewCheckboxColumn



2.

使用CurrentCellDirtyStateChanged事件将颜色更改代码放入内部

< pre lang =vb> Private void dgv1_ CurrentCellDirtyStateChanged Object sender, System.EventArgs e)
{
If (dgv1.IsCurrentCellDirty)
{
dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit )
如果(dgv1.CurrentCell.ColumnIndex == 1
{
oCell = dgv1.Rows [dgv1.CurrentCell.RowIndex] .Cells [ 0 ] as DataGridViewCheckBoxCell ;
bool bChecked =(null!= oCell&& null!= oCell.Value&& true ==(bool)oCell.Value) ;
if true == bChecked)
{
dgv1。行[dgv1.CurrentCell.RowIndex] .DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
dgv1.Rows [dgv1.CurrentCell.RowIndex] .DefaultCellStyle.BackColor = Color。绿色;
}
}
}
}



快乐编码!

:)


使用复选框控件向该网格视图添加模板化字段

eg->



 <   asp:TemplateField  > ;  
< ItemTemplate >
< asp:CheckBox ID = cb1 runat = < span class =code-keyword> server / >
< / ItemTemplate >
< / asp:TemplateField >





我提供以下示例来检索选中复选框的相应值





 受保护  void  Button1_Click(< span class =code-keyword> object  sender,EventArgs e)
{
int sum = 0 ;
for int i = 0 ; i < GridView1.Rows.Count; i ++)
{
CheckBox cb = GridView1.Rows [i] .FindControl(< span class =code-string>
cb1 as CheckBox;
if (cb.Checked)
{
sum + = Convert.ToInt32(GridView1.Rows [i] .Cells [< span class =code-digit> 2 ]。文字);
}
}
Label1.Text = sum.ToString();
}


hi everyone,
i am make a financial calculation in my application. it contains a 3 datagrid, it show 3 different credits and debits likes that, now my requirement is user want be HighLighted some rows in that Datagrid, Bcoz the highlighted rows some important debits/ credits. so now i decided to make checkbox selection to highlighted rows. when the transaction is not necessary remove selection in that chkbox it will be clear that row color.
i hope u all to understand my question.
how can i use gridview with checkbox with color changing:

解决方案

1. add DatagridviewCheckboxColumn

2.
Use CurrentCellDirtyStateChanged event put color changing code stuff inside

Private void dgv1_CurrentCellDirtyStateChanged(Object sender, System.EventArgs e) 
{
            If (dgv1.IsCurrentCellDirty)
            {
                dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit)
                If (dgv1.CurrentCell.ColumnIndex == 1)
                {  
                    oCell = dgv1.Rows[dgv1.CurrentCell.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                       bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);
                       if (true == bChecked)
                       {
                          dgv1.Rows[dgv1.CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                       }                  
                    else
                    {
                         dgv1.Rows[dgv1.CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.Green;
                    }
                }
            }
}


Happy Coding!
:)


add a templated field to that grid view with checkbox control
eg->

<asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="cb1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>



I''m providing the following example to retrieve the corresponding values where check boxes are checked


protected void Button1_Click(object sender, EventArgs e)
   {
       int sum = 0;
       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           CheckBox cb = GridView1.Rows[i].FindControl("cb1") as CheckBox;
           if (cb.Checked)
           {
               sum += Convert.ToInt32(GridView1.Rows[i].Cells[2].Text);
           }
       }
       Label1.Text = sum.ToString();
   }


这篇关于如何在CheckBox中使用DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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