Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Posted
Updated 6-May-13 22:04pm
v2

1. add DatagridviewCheckboxColumn

2.
Use CurrentCellDirtyStateChanged event put color changing code stuff inside
VB
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!
:)
 
Share this answer
 
v4
Comments
srigates 7-May-13 6:14am    
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
if (dataGridView1.CurrentCell.Value ==true) // error
{
dataGridView1.Rows(dataGridView1.CurrentCell.RowIndex).DefaultCellStyle.BackColor = Color.Yellow;

}
else
{
dataGridView1.Rows(dataGridView1.CurrentCell.RowIndex).DefaultCellStyle.BackColor = Color.Green;
}
}

}


}
it show the error like object '==' bool values and dgrv.Rows is show an error.
srigates 7-May-13 7:06am    
dataGridView1.Rows(dataGridView1.CurrentCell.RowIndex).DefaultCellStyle.BackColor = Color.Yellow;
it show error in dataGridView1.Rows//
Aarti Meswania 7-May-13 7:12am    
it's c# so use "[]" instead of "()"...
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
srigates 7-May-13 7:50am    
finally i got coloring in my grid, if remove check it does change to green color.

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{

if (dataGridView1.IsCurrentCellDirty)
{
if (dataGridView1.CurrentCell == null)
return;
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
if ((dataGridView1.CurrentCell.Value != null))
{

dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;

}
if ((dataGridView1.CurrentCell.Value == null))
{
{
dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.Green;
}
}
}



}


i am use this code.. it change color but it does not change to green color.
and boolean a=false;// here showing error.if change to Boolean and also show two more line error.
Aarti Meswania 7-May-13 7:56am    
above code is working or not?
add a templated field to that grid view with checkbox control
eg->

XML
<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


C#
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();
   }
 
Share this answer
 
Comments
srigates 7-May-13 6:02am    
i am asking in windows application...
Bikash Prakash Dash 7-May-13 6:08am    
oho sry , let me check

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900