Changing the width of columns in a table.
Hi guys, I have a hard one. I finally figurred out how to create a table in a datasset and load it up. However, one small problem, I have no idea how to change the widthof the columns since for my applications, they are too small and don't take up the width of the data grid. Could anyone help me specify the width of the columns.
I will past my code here on what i have so far: Hope no one rebukes me for pasting my code, its just I think if someone could help me they need to see my code that I have. I simple created a data set, created a table, added some values and added teh table to the data set. Please someone help me!!!
Code:
using System;
using System.Drawing;
using System.Collections; // Namespace for ArrayList.
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Table
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnInsert;
private System.Windows.Forms.DataGrid dgGrid; // Declare a Data grid.
DataTable myTable; // Declare a table.
DataRow myRow; // Declare a DataRow.
ArrayList Columns;
private System.Windows.Forms.TextBox txtFName;
private System.Windows.Forms.TextBox txtLName;
private System.Windows.Forms.Label lbl1;
private System.Windows.Forms.Label lbl2;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.TextBox txtDelete;
private System.Windows.Forms.Label lbl3; // ArrayList to store column names of table.
private System.ComponentModel.Container components = null;
public frmMain()
{
InitializeComponent();
// Use the MakeTable function below to create a new table.
Columns = new ArrayList();
Columns.Add("FirstName");
Columns.Add("LastName");
myTable = MakeNamesTable(); // Create new table with columns from ArrayList.
dgGrid.DataSource = myTable; // Add the table to the data grid.
}
private DataTable MakeNamesTable()
{
// Create a new DataTable titled 'Names.'
DataTable namesTable = new DataTable("Names");
// Access values for column names from ArrayList declared with global class scope.
for(int x=0 ; x<Columns.Count ; x++)
{
DataColumn Col = new DataColumn();
Col.DataType = System.Type.GetType("System.String");
Col.ColumnName = Columns[x].ToString();
Col.DefaultValue = Columns[x].ToString();
namesTable.Columns.Add(Col); // Add column to the table.
}/* 'for(int x=0 ; x<Columns.Count ; x++)' delimiter */
return (namesTable); // Return the table.
}
private void AddValues(ArrayList Values)
{
// Access values for column names from ArrayList declared with global class scope.
myRow = myTable.NewRow(); // Create a new DataRow.
for(int x=0 ; x<Columns.Count ; x++)
{
// Then add the new row to the collection.
myRow[Columns[x].ToString()] = Values[x].ToString();
}/* 'for(int x=0 ; x<Columns.Count ; x++)' delimiter */
myTable.Rows.Add(myRow); // Add the rows to the table.
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtFName = new System.Windows.Forms.TextBox();
this.btnInsert = new System.Windows.Forms.Button();
this.dgGrid = new System.Windows.Forms.DataGrid();
this.txtLName = new System.Windows.Forms.TextBox();
this.lbl1 = new System.Windows.Forms.Label();
this.lbl2 = new System.Windows.Forms.Label();
this.btnDelete = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.txtDelete = new System.Windows.Forms.TextBox();
this.lbl3 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dgGrid)).BeginInit();
this.SuspendLayout();
//
// txtFName
//
this.txtFName.Location = new System.Drawing.Point(184, 24);
this.txtFName.Name = "txtFName";
this.txtFName.Size = new System.Drawing.Size(112, 20);
this.txtFName.TabIndex = 0;
this.txtFName.Text = "";
//
// btnInsert
//
this.btnInsert.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnInsert.Location = new System.Drawing.Point(312, 24);
this.btnInsert.Name = "btnInsert";
this.btnInsert.Size = new System.Drawing.Size(136, 23);
this.btnInsert.TabIndex = 1;
this.btnInsert.Text = "Insert in Table";
this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
//
// dgGrid
//
this.dgGrid.DataMember = "";
this.dgGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgGrid.Location = new System.Drawing.Point(40, 112);
this.dgGrid.Name = "dgGrid";
this.dgGrid.Size = new System.Drawing.Size(368, 192);
this.dgGrid.TabIndex = 2;
//
// txtLName
//
this.txtLName.Location = new System.Drawing.Point(184, 64);
this.txtLName.Name = "txtLName";
this.txtLName.Size = new System.Drawing.Size(112, 20);
this.txtLName.TabIndex = 3;
this.txtLName.Text = "";
//
// lbl1
//
this.lbl1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbl1.Location = new System.Drawing.Point(56, 24);
this.lbl1.Name = "lbl1";
this.lbl1.Size = new System.Drawing.Size(100, 16);
this.lbl1.TabIndex = 4;
this.lbl1.Text = "FirstName";
//
// lbl2
//
this.lbl2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbl2.Location = new System.Drawing.Point(56, 64);
this.lbl2.Name = "lbl2";
this.lbl2.Size = new System.Drawing.Size(100, 16);
this.lbl2.TabIndex = 5;
this.lbl2.Text = "LastName";
//
// btnDelete
//
this.btnDelete.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnDelete.Location = new System.Drawing.Point(176, 336);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(152, 23);
this.btnDelete.TabIndex = 6;
this.btnDelete.Text = "Delete col from Table";
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnClear
//
this.btnClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnClear.Location = new System.Drawing.Point(360, 336);
this.btnClear.Name = "btnClear";
this.btnClear.TabIndex = 7;
this.btnClear.Text = "CLEAR";
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// txtDelete
//
this.txtDelete.Location = new System.Drawing.Point(40, 336);
this.txtDelete.Name = "txtDelete";
this.txtDelete.TabIndex = 8;
this.txtDelete.Text = "";
//
// lbl3
//
this.lbl3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbl3.Location = new System.Drawing.Point(16, 320);
this.lbl3.Name = "lbl3";
this.lbl3.Size = new System.Drawing.Size(152, 16);
this.lbl3.TabIndex = 9;
this.lbl3.Text = "Enter a column # to delete";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(456, 374);
this.Controls.Add(this.lbl3);
this.Controls.Add(this.txtDelete);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.lbl2);
this.Controls.Add(this.lbl1);
this.Controls.Add(this.txtLName);
this.Controls.Add(this.dgGrid);
this.Controls.Add(this.btnInsert);
this.Controls.Add(this.txtFName);
this.Name = "frmMain";
this.Text = "Table - by Darren Rattansingh";
((System.ComponentModel.ISupportInitialize)(this.dgGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void btnInsert_Click(object sender, System.EventArgs e)
{
ArrayList Values = new ArrayList(); // Create arraylist
// Add inputted values to the arraylist.
Values.Add(txtFName.Text);
Values.Add(txtLName.Text);
AddValues(Values); // Call method to insert values into the table.
/*----- Clear the textboxes -----*/
txtFName.Clear();
txtLName.Clear();
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
myTable.Rows.RemoveAt(Int32.Parse(txtDelete.Text)-1);
}
private void btnClear_Click(object sender, System.EventArgs e)
{
myTable.Clear(); // Clear the table.
}
}
}
Re: Changing the width of columns in a table.
Follow the Windows Forms FAQ link in my signature and have a look at the DataGrid section. It answers that and many other questions relating to the WinForms DataGrid, and many other controls too.
Re: Changing the width of columns in a table.
WOW!!! That collection is great, I think it's the MSDN 2!
Anyway here's what I got from you notes:
Code:
// Create a table style that will hold the new column style
// that we set and also tie it to our customer's table from our DB
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "customers";
//hide the column headers
tableStyle.ColumnHeadersVisible = false;
//change width of the row headers
tableStyle.RowHeadersWidth = 100;
// make the dataGrid use our new tablestyle and bind it to our table
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(tableStyle);
However, for some reason it was not working. I probably did something wrong. While searching, I saw that there is a property of the DataGrid in which you could specify the size of the columns and I did it like this:
DataGrid.PreferredColumnWidth = 120;
Do you know why your way was not working?
Jennifer