Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

big problem

Status
Not open for further replies.

ostelen

Programmer
Jan 30, 2005
5
FR
Hello,

I am using Visual .NET WInForms in C#

I actually have a problem with column of datagrid.
I just would change the size (width) of my column

I tried lot of things with tablestyles and posted on another forum, but nothing works !

This is my code, and it dont works ... does someone know why ?

DataGridTableStyle TSAuthors = new DataGridTableStyle();
TSAuthors.MappingName = "ds";
DataGridColumnStyle TCFirstName = new DataGridTextBoxColumn();
TCFirstName.MappingName = " AV_FName";
TCFirstName.HeaderText = "First Name";
TCFirstName.Width = 500;
TSAuthors.GridColumnStyles.Add(TCFirstName);
dataGrid1.TableStyles.Add(TSAuthors);
dataGrid1.Update();

 

Here are the steps to add a DataGridTableStyle:
1. Create a DataGridTableStyle object.
2. Set the grid table object's MappingName to a DataTable object's TableName.
3. Add DataGridColumnStyle objects, one for each grid column you want to show, to the GridColumnStylesCollection returned by the GridColumnStyles property.
4. Set the MappingName of each DataGridColumnStyle object to the ColumnName of a DataColumn.
5. Add the DataGridTableStyle object to the collection returned by TableStyles property.

In the example you posted you should fix the followings:
Code:
TSAuthors.MappingName = "existing table name";
TCFirstName.MappingName = "AV_FName"; //e.g. remove the spaces before AV.
Let me know the results.
obislavu


 
Thx for answer but it doesnt still work
this is my prog: (dont care, it is long but just copy / paste)

//the request
string text = "SELECT boite.id_boite as Id, boite.num_boite AS NumBoite, famille.nom_famille AS Famille, boite.libelle1 AS Lib1, boite.libelle2 AS Lib2, boite.libelle3 AS Lib3,
conteneur.num_conteneur AS Conteneur, boite.etat_boite AS Etat, boite.date_creation AS Creation, boite.date_expiration AS Destruction,
emplacement.site + emplacement.niveau + emplacement.travee + emplacement.[module] + emplacement.etagere AS Emplacement
FROM boite INNER JOIN
famille ON boite.id_famille = famille.id_famille INNER JOIN
conteneur ON boite.id_conteneur = conteneur.id_conteneur INNER JOIN
emplacement ON conteneur.id_emplacement = emplacement.id_emplacement INNER JOIN
societe ON boite.id_societe = societe.id_societe AND boite.id_societe = societe.id_societe
WHERE (societe.id_societe = '10051415') and boite.etat_boite <> 'NOUVELLE'"

//the connexion
sql_com = new SqlCommand();
sql_com.CommandText = text;
sql_com.Connection = sql_conn;
SqlDataAdapter sql_adapter = new SqlDataAdapter();
sql_adapter.SelectCommand = sql_com;
sql_conn.Close();
sql_conn.Open();
sql_adapter.Fill(v_ds);
sql_conn.Close();

dt2 = new DataTable("ds");
dt2 = v_ds.table[0];
//I delete de first column because i dont want to show it
dt2.Columns.RemoveAt(0);
//ALL OF BEFORE THAT LINE WORKS ! NOW THE REST DONT WORK (SIZE OF COLUMNS) :

//1.Create a DataGridTableStyle object.
DataGridTableStyle TSAuthors = new DataGridTableStyle();
//2.Set the grid table object's MappingName to a DataTable object's TableName.
TSAuthors.MappingName = "ds";
//3.Add DataGridColumnStyle objects, one for each grid column you want to show, to the GridColumnStylesCollection returned by the GridColumnStyles property.
//4.Set the MappingName of each DataGridColumnStyle object to the ColumnName of a DataColumn. DataGridColumnStyle TCFirstName = new DataGridTextBoxColumn();
TCFirstName.MappingName = "NumBoite";
TCFirstName.HeaderText = "NumBoite";
TCFirstName.Width = 10;
TSAuthors.GridColumnStyles.Add(TCFirstName);
DataGridColumnStyle col2 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Famille";
TCFirstName.HeaderText = "Famille";
TCFirstName.Width = 100;
TSAuthors.GridColumnStyles.Add(col2);
DataGridColumnStyle col3 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Lib1";
TCFirstName.HeaderText = "Lib1";
TCFirstName.Width = 10;
TSAuthors.GridColumnStyles.Add(col3);
DataGridColumnStyle col4 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Lib2";
TCFirstName.HeaderText = "Lib2";
TCFirstName.Width = 100;
TSAuthors.GridColumnStyles.Add(col4);
DataGridColumnStyle col5 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Lib3";
TCFirstName.HeaderText = "Lib3";
TCFirstName.Width = 10;
TSAuthors.GridColumnStyles.Add(col5);
DataGridColumnStyle col6 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Conteneur";
TCFirstName.HeaderText = "Conteneur";
TCFirstName.Width = 100;
TSAuthors.GridColumnStyles.Add(col6);
DataGridColumnStyle col7 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Etat";
TCFirstName.HeaderText = "Etat";
TCFirstName.Width = 10;
TSAuthors.GridColumnStyles.Add(col7);
DataGridColumnStyle col8 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Creation";
TCFirstName.HeaderText = "Creation";
TCFirstName.Width = 100;
TSAuthors.GridColumnStyles.Add(col8);
DataGridColumnStyle col9 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Destruction";
TCFirstName.HeaderText = "Destruction";
TCFirstName.Width = 10;
TSAuthors.GridColumnStyles.Add(col9);
DataGridColumnStyle col10 = new DataGridTextBoxColumn();
TCFirstName.MappingName = "Emplacement";
TCFirstName.HeaderText = "Emplacement";
TCFirstName.Width = 100;
TSAuthors.GridColumnStyles.Add(col10);
//5.Add the DataGridTableStyle object to the collection returned by TableStyles property. dataGrid1.TableStyles.Add(TSAuthors);


and ... onthing happened ... my data are here on the datagrid but the size didnt change

 
It is strange but I tried to reuse your code and here is how is working.
I created a table named _tTest with the all columns returned by your SQL statements.
I used the following:
Code:
SELECT  Id, NumBoite, Famille, Lib1, Lib2, Lib3, Conteneur, Etat, Creation, Destruction,Emplacement from _tTest
to have a returned set of data like yours.
Next, I have a form and there is a DataGrid object named dataGdid1, a button btnSQLgo and a label lblInfo and a txtSQL TextBox.
I typed the above SELECT in the txtSQL TextBox and click the btnSQLgo button.
The following code populate the grid with different column size.
Code:
private DataGrid dataGrid1;

private void InitializeComponent()
{
  //..
  this.dataGrid1 = new DataGrid();
  //..
}
private void btnSQLgo_Click(object sender, System.EventArgs e)
{
	TestTek();
}
private void TestTek()
{
	lblInfo.Text = "Retreiving data from " + txtSQL.Text.Trim() + "... On Moment Please...";
	
	DataSet ds = new DataSet("ds");
	DataTable dt2 = new DataTable("DT2");
	ds.Tables.Add(dt2);
	
	// Retrieve data from database
	string sSQLConnectionString = "....";
	FillDataSet(ref dt2,txtSQL.Text.Trim(),sSQLConnectionString);
	
	// Remove the first column (Id)
	dt2.Columns.RemoveAt(0);
	
	// Format grid
	FormatGridStyle(ref dataGrid1,dt2);

        // Bind data
	dataGrid1.SetDataBinding(ds, "DT2");
	//
	dataGrid1.CaptionText = dt2.Rows.Count + " Record(s) - " + txtSQL.Text;
	dataGrid1.GridLineStyle = DataGridLineStyle.Solid;
	// Show grid
	dataGrid1.Show();

	dataGrid1.BackgroundColor = System.Drawing.Color.YellowGreen;
	dataGrid1.ColumnHeadersVisible = true;
	lblInfo.Text = "Done.";
	
	
}


private void FormatGridStyle(ref DataGrid dg, DataTable dt)
{
	dg.TableStyles.Clear();
	DataGridTableStyle TSAuthors  = new DataGridTableStyle();
	TSAuthors .MappingName = dt.TableName;
	TSAuthors .AlternatingBackColor=System.Drawing.Color.Bisque;
	TSAuthors .AlternatingBackColor=System.Drawing.Color.LightYellow;
	TSAuthors .BackColor = System.Drawing.Color.White;
	TSAuthors .GridLineStyle=System.Windows.Forms.DataGridLineStyle.Solid;
	foreach(DataColumn vColumn in dt.Columns)
	{
		DataGridTextBoxColumn vColumnStyle= new DataGridTextBoxColumn();
		vColumnStyle.HeaderText = vColumn.ColumnName;
		vColumnStyle.MappingName = vColumn.ColumnName ;
		vColumnStyle.Width = 60;  // 10 is too small
		switch(vColumn.ColumnName)
		{
			case "Famille":
			case "Lib2":
			case "Conteneur":
			case "Emplacement":
			vColumnStyle.Width = 200;
				break;
		}
		TSAuthors.GridColumnStyles.Add(vColumnStyle);
	}
	dg.TableStyles.Add(TSAuthors);  
}	

public void FillDataSet(ref DataTable dt, string sqlStatement, string sConnString)
{
	SqlConnection sConn		= null;
	SqlCommand WorkingCommand	= null;
	SqlDataAdapter WorkingDataAdapter= null;
	try
	{
		sConn		= new SqlConnection(sConnString);
		WorkingCommand	= new SqlCommand();
		WorkingDataAdapter	= new SqlDataAdapter();
		sConn.Open();
		WorkingCommand.Connection= sConn;
		WorkingCommand.CommandText = sqlStatement;
		WorkingDataAdapter.SelectCommand = WorkingCommand;
		WorkingDataAdapter.Fill(dt);

	}
	catch(Exception e)
	{						
		string sErr=e.GetType() + e.Message;
	}						
	finally 
	{
		if (sConn!=null)
		{
			sConn.Close();
			sConn.Dispose();
		}
	}

}
obislavu
 
Thank you a lot for your answer obislavu,

Now it is working.
I give you the reason of my problem for archiving:
i didnt affect the good mapping name

The bad way(that i used):
DataTable dt = new DataTable("ds");
DgTableStyle.MappingName = "ds" ;

The good way(working):
DataTable dt = new DataTable("ds");
DgTableStyle.MappingName = dt.TableName ;

Then for each column affect the good mapping name which correspond to the name of the column in the request

Thx again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top