misscricri

Hi!

I am working with an sql database that it's loading into a datagrid. I would like to personalize my datagrid and add checkboses on the first column.

There is my code :

public class PersonnalisationDataGrid : Form_Consult

{

private System.Windows.Forms.DataGridTextBoxColumn Column1;

//private System.Windows.Forms.DataGridTextBoxColumn Column2;

//private System.Windows.Forms.DataGridTextBoxColumn Column3;

public PersonnalisationDataGrid()

{

//

// TODO: Add constructor logic here

//

this.Column1.HeaderText = "Column1";

}

private void CreateMyDatagrid()

{

//DataGrid MyDataGrid = new DataGrid();

dataGrid1.Bounds = new Rectangle(new Point(10,10), new Size(650,350));

//dataGrid1.CaptionText="Berechtigung";

//DataSet MyDataSet = new DataSet();

//DataGridTableStyle myTableStyle = new DataGridTableStyle();

tableStyle.MappingName = "MappingName_ts_berechtigung";

DataGridBoolColumn boolCol = new DataGridBoolColumn();

boolCol.MappingName="ALLE";

((DataGridBoolColumn)boolCol).AllowNull = false;

tableStyle.GridColumnStyles.Add(boolCol);

//dataGrid1.TableStyles.Clear();

//dataGrid1.TableStyles.Add(myTableStyle);

DataTable dt = new DataTable("DataTable_Berechtigung");

dataGrid1.TableStyles["DataTable_Berechtigung"].GridColumnStyles["ALLE"].Width = 250;

dt.Columns.Add("Column1", typeof(string)).ReadOnly = true;

dt.Columns.Add("Column2", typeof(string)).ReadOnly = true;

dt.Columns.Add("Column3", typeof(bool));

dt.Columns.Add("Column4", typeof(bool));

dt.Columns.Add("Bearbeiten", typeof(bool));

dt.Columns.Add("Erstellen", typeof(bool));

dt.Columns.Add("Loschen", typeof(bool));

dataGrid1.TableStyles["DataTable_Berechtigung"].GridColumnStyles[0].Width=0;

dataGrid1.TableStyles["DataTable_Berechtigung"].GridColumnStyles[1].Width=80;

dataGrid1.TableStyles["DataTable_Berechtigung"].GridColumnStyles[2].Width=80;

dataGrid1.TableStyles["DataTable_Berechtigung"].GridColumnStyles[3].Width=20;

// checkbox in variable

int test = 1;

dt.Rows.Add(new Object[] {"Adresse", "ev. sondst noch was",0,test,0,0,0});

dt.Rows.Add(new Object[] {"Depositen", "ev. sondst noch was",1,1,1,1,1});

dt.Rows.Add(new Object[] {"Obligation", "ev. sondst noch was",0,0,0,0,0});

dt.Rows.Add(new Object[] {"Miete", "ev. sondst noch was",1,1,1,1,1});

dt.Rows.Add(new Object[] {"Stammdaten", "ev. sondst noch was",0,0,0,0,0});

// DataTable in dataset

myDataset.Tables.Add(dt);

// Dataset in datagrid

dataGrid1.SetDataBinding(myDataset,"DataTable_Berechtigung");

this.Controls.Add(dataGrid1);

}

}

When i try to compile the applicatio, i have an error with this definition:

this.dataGrid1.Column1.Add(new System.Windows.Forms.DataGridBoolColumn[] {this.Column1});

Can someone help me

Thanks a lot!




Re: Windows Forms Data Controls and Databinding Checkbox + datagrid

Ken Tucker

You need to add columns to datagrid's TableStyle. There is an example of adding a tablestyle to a datagrid here.





Re: Windows Forms Data Controls and Databinding Checkbox + datagrid

misscricri

It works... Thank you... I do have checkboxes on a column but i have now to handle the state and i would like not to have a header text on the column..

Thanks






Re: Windows Forms Data Controls and Databinding Checkbox + datagrid

Zhi-Xin Ye - MSFT

misscricri wrote:

It works... Thank you... I do have checkboxes on a column but i have now to handle the state and i would like not to have a header text on the column..

Thanks

Try this code:

private void Form3_Load(object sender, System.EventArgs e)

{

DataTable dtBook = new DataTable("dtBook");

dtBook.Columns.Add("status", typeof(bool));

dtBook.Columns.Add("bookid", typeof(int));

dtBook.Columns.Add("bookname", typeof(string));

for (int i = 0; i < 10; i++)

{

DataRow dr = dtBook.NewRow();

dr[1] = i;

dr[2] = "bo" +i.ToString() +"ok" + i.ToString();

dtBook.Rows.Add(dr);

}

this.dataGrid1.DataSource = dtBook;

DataGridTableStyle tbStyle = new DataGridTableStyle();

tbStyle.MappingName = "dtBook";

this.dataGrid1.TableStyles.Add(tbStyle);

this.dataGrid1.TableStyles["dtBook"].GridColumnStyles[0].HeaderText = "";

}

Put attention to the statement in bold.

Regards,

Ye