CheckBox Column in a DataGrid ?
// Does anyone have a working example of how to create a
// DataGrid containing a column of CheckBoxes? The code
// below is clearly not working -- "True" shows in the
// first column (the bool column), rather than CheckBoxes.
// TIA, Jim H.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// "Plug-n-Play" code. Provides a working example of creating
// an in-memory Dataset and binding it to
// a DataGrid.
// Need to have Column1 in DataGrid1 be a CheckBox
// Note that Column1 of the recStruct is type bool
namespace DataGrid103
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Data;
public class Form1 : System.WinForms.Form
{
public struct recStruct
{
public bool Column1; // Must be public scope
public string Column2; // Must be public scope
};
DataSet dsDir1 = new DataSet( "dsDir1" );
DataTable dtDir1 = new DataTable( "dtDir1" );
private System.ComponentModel.Container components;
private System.WinForms.DataGrid dataGrid1;
public Form1() // Constructor
{
InitializeComponent();
createDataSet1();
loadDataTable1();
// Now Bind DataGrid1 to DataSet1, DataTable1
dataGrid1.SetDataBinding( dsDir1, "dtDir1" ); // Args. are DataSetName, DataTableName
dataGrid1.SelectedIndex = 0; // Navigate to top of listBox
}
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.dataGrid1 = new System.WinForms.DataGrid ();
dataGrid1.BeginInit ();
dataGrid1.Location = new System.Drawing.Point (16, 16);
dataGrid1.Size = new System.Drawing.Size (248, 216);
dataGrid1.DataMember = "";
dataGrid1.TabIndex = 0;
this.Text = "Form1";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.Controls.Add (this.dataGrid1);
dataGrid1.EndInit ();
}
public static void Main(string[] args)
{
Application.Run( new Form1() );
}
protected void createDataSet1()
{
// Add the DataTable to the DataSet
dsDir1.Tables.Add( dtDir1 );
// Create DataColumns for FileName, DateTime, Size
DataColumn column1 = new DataColumn( "Column 1" );
DataColumn column2 = new DataColumn( "Column 2" );
// HowTo Set Column Width of a Column in a DataGrid ???
// Add the DataColums to the DataTable
dsDir1.Tables["dtDir1"].Columns.Add( column1 );
dsDir1.Tables["dtDir1"].Columns.Add( column2 );
}
private void loadDataTable1()
{
DataRow nr;
for ( int i = 0; i <= 3; i++ )
{
// Create and add a new row to the DataTable
nr = dtDir1.NewRow();
nr["Column 1"] = true;
nr["Column 2"] = "Record " + (i + 1);
dtDir1.Rows.Add( nr );
}
}
}
}
Re: CheckBox Column in a DataGrid ?
Hi JimH !
Do you find the solution ( how to add CheckBox or other control to
DataGrid ) ?
Please mail me.
Thanks,
Stan
e-mail: stan72@mail.ru
Jim H wrote:
>
> // Does anyone have a working example of how to create a
> // DataGrid containing a column of CheckBoxes? The code
> // below is clearly not working -- "True" shows in the
> // first column (the bool column), rather than CheckBoxes.
>
> // TIA, Jim H.
>
> // - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
> // "Plug-n-Play" code. Provides a working example of creating
> // an in-memory Dataset and binding it to
> // a DataGrid.
> // Need to have Column1 in DataGrid1 be a CheckBox
> // Note that Column1 of the recStruct is type bool
>
> namespace DataGrid103
> {
> using System;
> using System.Drawing;
> using System.Collections;
> using System.ComponentModel;
> using System.WinForms;
> using System.Data;
>
> public class Form1 : System.WinForms.Form
> {
>
> public struct recStruct
> {
> public bool Column1; // Must be public scope
> public string Column2; // Must be public scope
> };
>
> DataSet dsDir1 = new DataSet( "dsDir1" );
> DataTable dtDir1 = new DataTable( "dtDir1" );
>
> private System.ComponentModel.Container components;
> private System.WinForms.DataGrid dataGrid1;
>
> public Form1() // Constructor
> {
> InitializeComponent();
> createDataSet1();
> loadDataTable1();
> // Now Bind DataGrid1 to DataSet1, DataTable1
> dataGrid1.SetDataBinding( dsDir1, "dtDir1" ); // Args. are DataSetName, DataTableName
> dataGrid1.SelectedIndex = 0; // Navigate to top of listBox
> }
>
> public override void Dispose()
> {
> base.Dispose();
> components.Dispose();
> }
>
> private void InitializeComponent()
> {
> this.components = new System.ComponentModel.Container ();
> this.dataGrid1 = new System.WinForms.DataGrid ();
> dataGrid1.BeginInit ();
> dataGrid1.Location = new System.Drawing.Point (16, 16);
> dataGrid1.Size = new System.Drawing.Size (248, 216);
> dataGrid1.DataMember = "";
> dataGrid1.TabIndex = 0;
> this.Text = "Form1";
> this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
> this.Controls.Add (this.dataGrid1);
> dataGrid1.EndInit ();
> }
>
> public static void Main(string[] args)
> {
> Application.Run( new Form1() );
> }
>
> protected void createDataSet1()
> {
> // Add the DataTable to the DataSet
> dsDir1.Tables.Add( dtDir1 );
>
> // Create DataColumns for FileName, DateTime, Size
> DataColumn column1 = new DataColumn( "Column 1" );
> DataColumn column2 = new DataColumn( "Column 2" );
>
> // HowTo Set Column Width of a Column in a DataGrid ???
>
> // Add the DataColums to the DataTable
> dsDir1.Tables["dtDir1"].Columns.Add( column1 );
> dsDir1.Tables["dtDir1"].Columns.Add( column2 );
> }
>
> private void loadDataTable1()
> {
> DataRow nr;
> for ( int i = 0; i <= 3; i++ )
> {
> // Create and add a new row to the DataTable
> nr = dtDir1.NewRow();
> nr["Column 1"] = true;
> nr["Column 2"] = "Record " + (i + 1);
> dtDir1.Rows.Add( nr );
> }
> }
> }
> }
--
Sent via http://csharpindex.com
C# Index to resources