wtdoor
12-08-2004, 10:44 AM
I am trying to use a datagrid with columns of different width. I have found several methods posted on various sites, but unfortunately am unable to make any of them work. The following code seems to be the most straightforward, but none of the formatting is actually applied to the datagrid.
What am I missin?
private void FormView_Load(object sender, System.EventArgs e)
{
string connection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=reminder.mdb";
//string query ="SELECT Date,Time,Message,TicklerDate,TicklerTime FROM Ticklers";
string query ="SELECT * FROM Ticklers";
// Initialize the connection using the connection string
conn = new OleDbConnection(connection);
// Open the db connection
conn.Open();
//Initiate the OleDbDataAdapter using the SQL select commmand on the db connection
adapter=new OleDbDataAdapter(query,conn);
// Initiate and fill the dataset
ds = new DataSet();
//initiate the datatable
adapter.Fill(ds,"Customers");
dt = ds.Tables[0];
// Get the database schema
adapter.FillSchema(dt, SchemaType.Source);
conn.Close();
// Format the data grid
DataGridTableStyle ts = new DataGridTableStyle();
this.dataGrid1.TableStyles.Clear();
//ts.MappingName="Ticklers";
ts.MappingName=dataGrid1.DataMember;
DataGridTextBoxColumn column1 = new DataGridTextBoxColumn();
column1.MappingName="Index";
column1.HeaderText="Index";
column1.Width = 0;
ts.GridColumnStyles.Add(column1);
DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
column2.MappingName="Date";
column2.HeaderText="Event Date";
column2.Width = 75;
ts.GridColumnStyles.Add(column2);
DataGridTextBoxColumn column3 = new DataGridTextBoxColumn();
column3.MappingName="Time";
column3.HeaderText="Event Time";
column3.Width = 75;
ts.GridColumnStyles.Add(column3);
DataGridTextBoxColumn column4 = new DataGridTextBoxColumn();
column4.MappingName="Message";
column4.HeaderText="Description";
column4.Width = 150;
ts.GridColumnStyles.Add(column4);
DataGridTextBoxColumn column5 = new DataGridTextBoxColumn();
column5.MappingName="TicklerTime";
column5.HeaderText="Tickler Time";
column5.Width = 75;
ts.GridColumnStyles.Add(column5);
DataGridTextBoxColumn column6 = new DataGridTextBoxColumn();
column6.MappingName="TicklerDate";
column6.HeaderText="Tickler Date";
column6.Width = 75;
ts.GridColumnStyles.Add(column6);
dataGrid1.TableStyles.Add(ts);
// Bind the datatable to the datagrid control
this.dataGrid1.DataSource=dt;
// Show the datagrid
this.dataGrid1.Visible=true;
}
What am I missin?
private void FormView_Load(object sender, System.EventArgs e)
{
string connection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=reminder.mdb";
//string query ="SELECT Date,Time,Message,TicklerDate,TicklerTime FROM Ticklers";
string query ="SELECT * FROM Ticklers";
// Initialize the connection using the connection string
conn = new OleDbConnection(connection);
// Open the db connection
conn.Open();
//Initiate the OleDbDataAdapter using the SQL select commmand on the db connection
adapter=new OleDbDataAdapter(query,conn);
// Initiate and fill the dataset
ds = new DataSet();
//initiate the datatable
adapter.Fill(ds,"Customers");
dt = ds.Tables[0];
// Get the database schema
adapter.FillSchema(dt, SchemaType.Source);
conn.Close();
// Format the data grid
DataGridTableStyle ts = new DataGridTableStyle();
this.dataGrid1.TableStyles.Clear();
//ts.MappingName="Ticklers";
ts.MappingName=dataGrid1.DataMember;
DataGridTextBoxColumn column1 = new DataGridTextBoxColumn();
column1.MappingName="Index";
column1.HeaderText="Index";
column1.Width = 0;
ts.GridColumnStyles.Add(column1);
DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
column2.MappingName="Date";
column2.HeaderText="Event Date";
column2.Width = 75;
ts.GridColumnStyles.Add(column2);
DataGridTextBoxColumn column3 = new DataGridTextBoxColumn();
column3.MappingName="Time";
column3.HeaderText="Event Time";
column3.Width = 75;
ts.GridColumnStyles.Add(column3);
DataGridTextBoxColumn column4 = new DataGridTextBoxColumn();
column4.MappingName="Message";
column4.HeaderText="Description";
column4.Width = 150;
ts.GridColumnStyles.Add(column4);
DataGridTextBoxColumn column5 = new DataGridTextBoxColumn();
column5.MappingName="TicklerTime";
column5.HeaderText="Tickler Time";
column5.Width = 75;
ts.GridColumnStyles.Add(column5);
DataGridTextBoxColumn column6 = new DataGridTextBoxColumn();
column6.MappingName="TicklerDate";
column6.HeaderText="Tickler Date";
column6.Width = 75;
ts.GridColumnStyles.Add(column6);
dataGrid1.TableStyles.Add(ts);
// Bind the datatable to the datagrid control
this.dataGrid1.DataSource=dt;
// Show the datagrid
this.dataGrid1.Visible=true;
}