C# Tutorials and offshore development in India

Tutorials Resources Forum Reviews Interview Jobs Projects Training Your Ad Here


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...




Forums » .NET » .NET »

add check box in datagridview


Posted Date: 09 Aug 2007      Posted By: abirami      Member Level: Gold     Points: 2   Responses: 3



Hi

im working in c#.net. im using datagridview, i want to add a checkbox at the end of the
all the columns.while im running my application if i tick the checkbox it should store in database how to do that?





Responses

Author: sonali    09 Aug 2007Member Level: BronzeRating: 2 out of 52 out of 5     Points: 2

hi
hope this will help u
this code will help u to even check and uncheck the check box property



using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView();

[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}

public Form1()
{
this.AutoSize = true;
this.Load += new EventHandler(Form1_Load);
}

public void Form1_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn column0 =
new DataGridViewCheckBoxColumn();
DataGridViewDisableCheckBoxColumn column1 =
new DataGridViewDisableCheckBoxColumn();
column0.Name = "CheckBoxes";
column1.Name = "DisableCheckBoxes";
dataGridView1.Columns.Add(column0);
dataGridView1.Columns.Add(column1);
dataGridView1.RowCount = 8;
dataGridView1.AutoSize = true;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;

// Set the text for each checkBox.
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1.Rows.Cells["DisableCheckBoxes"].Value =
false;
}

dataGridView1.CellValueChanged +=
new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged +=
new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
dataGridView1.CellClick +=
new DataGridViewCellEventHandler(dataGridView1_CellClick);

this.Controls.Add(dataGridView1);
}

// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

// If a check box cell is clicked, this event handler disables
// or enables the checkBox in the same row as the clicked cell.
public void dataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
{
DataGridViewDisableCheckBoxCell checkBoxCell =
(DataGridViewDisableCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["DisableCheckBoxes"];

DataGridViewCheckBoxCell checkCell =
(DataGridViewCheckBoxCell)dataGridView1.
Rows[e.RowIndex].Cells["CheckBoxes"];
checkBoxCell.Enabled = !(Boolean)checkCell.Value;

dataGridView1.Invalidate();
}
}

SONAL



Author: abirami    09 Aug 2007Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Thank u very much. its veryusefull for me.


Author: dhirander Kumar    24 Dec 2008Member Level: BronzeRating: 2 out of 52 out of 5     Points: 0

This is really very helpful..... Thanx a lot sonal


Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Loading crystal report taking time. Can anyone help me solving the prob?
Previous : checkbox uncheck property at run time
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages




About Us    Contact Us    Privacy Policy    Terms Of Use