Help with Windows DataGrid and DataBinding (Urgent)

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hello all,
I'm trying to display a dataset into a datagrid and format the datagrid
columns but I'm having a hard time with it. I've tried the MSDN examples but
they don't seem to work.

Basically, what I want to do is that, on a button click, the dataset fills
up dynamically, binds to the datagrid, and displays the data. This is my
code:

mySql = "select itemID, OrderDate " +
"from ClientTrans where " +
"clientID = " + txtClientID.Text;
DBConnection DBConn = new DBConnection("SQLServer", mySql);
DataSet DSMyTrans = DBConn.Connection();

//I use either one of the following two for the databinding
//DGOrders.DataSource = MyTransDS.Tables["MyTable"].DefaultView;
//DGOrders.SetDataBinding(DSMyTrans, "");

I only need some way to format the columns in the datagrid (column width,
headertext, etc...) and I've searched through MSDN but to no avail. I've
tried TableStyles:
DataGridTableStyle myTS = new DataGridTableStyle();
ts1.MappingName = "Clients";


DataGridBoolColumn myDataCol = new DataGridBoolColumn();
myDataCol.HeaderText = "From Date";
myDataCol.MappingName = "Current";

ts1.GridColumnStyles.Add(myDataCol);
DGOrders.TableStyles.Add(ts1);

but nothing happens.
Are there any links that show how to format datagrids (from the dataset
fill)?

John
 
John,

Well, if you cut and pasted your code then I guess you're not using the
right variable. First you're using ' myTS ' and then you're referring to '
ts1 ' in the next line of code.

DataGridTableStyle myTS = new DataGridTableStyle();
ts1.MappingName = "Clients";

Could that be the problem?

Regards, Jim
 
What sort of application is that? Windows Form? Web Form? What is in your
DBConnection class?
 
Sorry. Since I was in hurry, I made that mistake.
Here's the code for the datagrid in the Windows form:

string mySql;
mySql = "select itemID " +
"from ClientTrans where " +
"clientID = " + txtClientID.Text;
DataSet DSMyTrans = new DataSet();
SqlConnection SQLConn = new SqlConnection("Data Source=localhost; Integrated
Security=SSPI;" +
"Initial Catalog=invoicing");
SqlDataAdapter MyDataAdapterClients = new SqlDataAdapter (mySql, SQLConn);
MyDataAdapterClients.TableMappings.Add("Table","MyTable");
MyDataAdapterClients.Fill(DSMyTrans);
DGOrders.DataSource = DSMyTrans.Tables["MyTable"].DefaultView;
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Clients";
DataGridBoolColumn myDataCol = new DataGridBoolColumn();
myDataCol.HeaderText = "From Date";
myDataCol.MappingName = "Current";
ts1.GridColumnStyles.Add(myDataCol);
DGOrders.TableStyles.Add(ts1);

I can see the data in the datagrid but I would like to change the different
column properties (the headertext, column width, etc...) dyamically? I've
tried tons of way to change the column header text but nothing happens.
Thanks,
John
 
It's a Windows form.
Here's the code with the connection..

string mySql;
mySql = "select itemID " +
"from ClientTrans where " +
"clientID = " + txtClientID.Text;
DataSet DSMyTrans = new DataSet();
SqlConnection SQLConn = new SqlConnection("Data Source=localhost; Integrated
Security=SSPI;" +
"Initial Catalog=invoicing");
SqlDataAdapter MyDataAdapterClients = new SqlDataAdapter (mySql, SQLConn);
MyDataAdapterClients.TableMappings.Add("Table","MyTable");
MyDataAdapterClients.Fill(DSMyTrans);
DGOrders.DataSource = DSMyTrans.Tables["MyTable"].DefaultView;
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Clients";
DataGridBoolColumn myDataCol = new DataGridBoolColumn();
myDataCol.HeaderText = "From Date";
myDataCol.MappingName = "Current";
ts1.GridColumnStyles.Add(myDataCol);
DGOrders.TableStyles.Add(ts1);

Thanks,
John

Eliyahu Goldin said:
What sort of application is that? Windows Form? Web Form? What is in your
DBConnection class?

John said:
Hello all,
I'm trying to display a dataset into a datagrid and format the datagrid
columns but I'm having a hard time with it. I've tried the MSDN examples but
they don't seem to work.

Basically, what I want to do is that, on a button click, the dataset fills
up dynamically, binds to the datagrid, and displays the data. This is my
code:

mySql = "select itemID, OrderDate " +
"from ClientTrans where " +
"clientID = " + txtClientID.Text;
DBConnection DBConn = new DBConnection("SQLServer", mySql);
DataSet DSMyTrans = DBConn.Connection();

//I use either one of the following two for the databinding
//DGOrders.DataSource = MyTransDS.Tables["MyTable"].DefaultView;
//DGOrders.SetDataBinding(DSMyTrans, "");

I only need some way to format the columns in the datagrid (column width,
headertext, etc...) and I've searched through MSDN but to no avail. I've
tried TableStyles:
DataGridTableStyle myTS = new DataGridTableStyle();
ts1.MappingName = "Clients";


DataGridBoolColumn myDataCol = new DataGridBoolColumn();
myDataCol.HeaderText = "From Date";
myDataCol.MappingName = "Current";

ts1.GridColumnStyles.Add(myDataCol);
DGOrders.TableStyles.Add(ts1);

but nothing happens.
Are there any links that show how to format datagrids (from the dataset
fill)?

John
 
Back
Top