.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

Checkboxes in DataGridView using C#

Posted By: Naim Khan     Posted Date: September 07, 2009    Points:2   Category :C#
Hi,
Thanks for the reply but dats not my requirement, what you have given is too basic I need checkbox at header too... pl z read my requirement once again.

i m using Check boxes in datagridview for one to many relationship. default should be have one checked. and other that checked by user well i m posting my code which i have made it.

// this is the Code....

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using BussinessLayer;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
BussinessLayer.BussinessLayer gBL = new BussinessLayer.BussinessLayer();
public Form1()
{
InitializeComponent();
}
DGVColumnHeader dgvColumnHeader;
private void Form1_Load(object sender, EventArgs e)
{
dgvColumnHeader = new DGVColumnHeader();

//Add columns dynamically to gridview
dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());
dataGridView1.Columns[0].HeaderCell = dgvColumnHeader;

//Data Binding
show_student_details();


}

private void show_student_details()
{
try
{
DataSet ds1 = new DataSet();
ds1 = gBL.Show_student_details();
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds1.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("Error!: " + ex.Message.ToString() + ". Please contact PSR Computers for Support");
}
}

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{

}


private void dataGridView1_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)
{
/* List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells[id.Name].Value) == true)
{
rows_with_checked_column.Add(row);
}
}*/
}


private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = dgvColumnHeader.CheckAll;
}
}
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
//Escalate Editmode
this.dataGridView1.EndEdit();
string re_value = this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();
this.dataGridView1.Rows[i].Cells[0].Value = "true";
}
}
}
}
}


in this code i have created a class.......
code of class

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsApplication1
{
class DGVColumnHeader : DataGridViewColumnHeaderCell
{
private Rectangle CheckBoxRegion;
private bool checkAll = false;

protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{

base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value,
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);

CheckBoxRegion = new Rectangle(
cellBounds.Location.X + 1,
cellBounds.Location.Y + 2,
25, cellBounds.Size.Height - 4);


if (this.checkAll)
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Checked);
else
ControlPaint.DrawCheckBox(graphics, CheckBoxRegion, ButtonState.Normal);

Rectangle normalRegion =
new Rectangle(
cellBounds.Location.X + 1 + 25,
cellBounds.Location.Y,
cellBounds.Size.Width - 26,
cellBounds.Size.Height);

graphics.DrawString(value.ToString(), cellStyle.Font, new SolidBrush(cellStyle.ForeColor), normalRegion);
}

protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//Convert the CheckBoxRegion
Rectangle rec = new Rectangle(new Point(0, 0), this.CheckBoxRegion.Size);
this.checkAll = !this.checkAll;
if (rec.Contains(e.Location))
{
this.DataGridView.Invalidate();
}
base.OnMouseClick(e);
}

public bool CheckAll
{
get { return this.checkAll; }
set { this.checkAll = value; }
}
}

}


pl z reply me as soon as possible. i hope u get it my requirement.

Responses
Author: Sagar Pardeshi             
Posted Date: February 10, 2010     Points: 5   


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