.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Unchecking a checkbox within a datagridview using c#.net (in windows application)

Posted By: sravanthi     Posted Date: March 14, 2011    Points:5   Category :C#
i took a datagridview with alternative rows default cell styling and within that i took a checkbox..if i check that checkbox, the particular rows has to change it to color:yellow.and if i uncheck the same checkbox ,the color has to change it to default color based on the row..
i written the below code for checkng a checkbox,it's working fine..
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{
if (e.ColumnIndex == 2)
{
AddClause obj = new AddClause();
obj.Show();

}

if (e.ColumnIndex == 0)
{

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{

if (dataGridView1.Rows[i].Cells["Column1"].Selected)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}

}


}

}

I need coding for unchecking the checkbox within a datagridview..using c#( in windows application)


Responses
Author: Arul             
Posted Date: March 15, 2011     Points: 20   

Hi,

your code to validate selected check box is wrong. You are changing the color on cell selected.

Author: sravanthi             
Posted Date: March 15, 2011     Points: 20   

hi Arul,

I need code for unchecking a checkbox within datagridview..
i checked your 2 links..but nothing is available there
plz let me know how to uncheck a checkbox within datagridview


Author: Maverick             
Posted Date: March 15, 2011     Points: 20   

Hello,

Use the follwing javascript.

function CheckAll(chkBoxId, gridId) {
var grid = document.getElementById(gridId);
var cell;
if (grid.rows.length > 0) {
for (i = 1; i < grid.rows.length; i++) {
cell = grid.rows[i].cells[0];
for (j = 0; j < cell.childNodes.length; j++) {
if (cell.childNodes[j].type == "checkbox") {
cell.childNodes[j].checked = document.getElementById(chkBoxId).checked;
}
}
}
}
}
Thanks,
Maverick

Thanks,
Maverick
Author: Arul             
Posted Date: March 15, 2011     Points: 20   

Hi,

Try this..

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
AddClause obj = new AddClause();
obj.Show();
}
if (e.ColumnIndex == 0)
{

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)dataGridView1.Rows[i].Cells["Column1"].FindControl("chk1"));//chk1 is id of check box

if (chk.checked)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
//set default color
}
}
}
}

Thanks,
Arul



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend