How to change the column width in a dataGrid?


Hello,

I am trying to change the column width in a dataGrid.

It's not working... when I click the button, nothing changes on the grid...

what am I missing?

---

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Agenda
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class FrmPrincipal : System.Windows.Forms.Form
      {
            private System.Data.OleDb.OleDbConnection dbConn;
            private System.Data.OleDb.OleDbDataAdapter dbDataAdapter;
            private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
            private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
            private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
            private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
            private System.Data.DataSet dataSet;
            private System.Windows.Forms.DataGrid dataGrid;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.DataGridTableStyle dgTableStyle;
            private System.Windows.Forms.DataGridTextBoxColumn dgTextBoxColumn0;
            private System.Windows.Forms.DataGridTextBoxColumn dgTextBoxColumn1;
            private System.Windows.Forms.DataGridTextBoxColumn dgTextBoxColumn2;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public FrmPrincipal()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();
                  dbDataAdapter.Fill(dataSet);
                  dataGrid.DataSource = dataSet.Tables["compromissos"];                  
                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <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.dbConn = new System.Data.OleDb.OleDbConnection();
                  this.dbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
                  this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
                  this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
                  this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
                  this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
                  this.dataSet = new System.Data.DataSet();
                  this.dataGrid = new System.Windows.Forms.DataGrid();
                  this.button1 = new System.Windows.Forms.Button();
                  this.dgTableStyle = new System.Windows.Forms.DataGridTableStyle();
                  this.dgTextBoxColumn0 = new System.Windows.Forms.DataGridTextBoxColumn();
                  this.dgTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
                  this.dgTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
                  ((System.ComponentModel.ISupportInitialize)(this.dataSet)).BeginInit();
                  ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
                  this.SuspendLayout();
                  //
                  // dbConn
                  //
                  this.dbConn.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=""agenda.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False";
                  //
                  // dbDataAdapter
                  //
                  this.dbDataAdapter.DeleteCommand = this.oleDbDeleteCommand1;
                  this.dbDataAdapter.InsertCommand = this.oleDbInsertCommand1;
                  this.dbDataAdapter.SelectCommand = this.oleDbSelectCommand1;
                  this.dbDataAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                                                                                                                                                      new System.Data.Common.DataTableMapping("Table", "compromissos", new System.Data.Common.DataColumnMapping[] {
                                                                                                                                                                                                                                                                                                                              new System.Data.Common.DataColumnMapping("datahora_compromisso", "datahora_compromisso"),
                                                                                                                                                                                                                                                                                                                              new System.Data.Common.DataColumnMapping("desc_compromisso", "desc_compromisso"),
                                                                                                                                                                                                                                                                                                                              new System.Data.Common.DataColumnMapping("id_compromisso", "id_compromisso")})});
                  this.dbDataAdapter.UpdateCommand = this.oleDbUpdateCommand1;
                  //
                  // oleDbDeleteCommand1
                  //
                  this.oleDbDeleteCommand1.CommandText = "DELETE FROM compromissos WHERE (id_compromisso = ?) AND (datahora_compromisso = ?" +
                        " OR ? IS NULL AND datahora_compromisso IS NULL) AND (desc_compromisso = ?)";
                  this.oleDbDeleteCommand1.Connection = this.dbConn;
                  this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_id_compromisso", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "id_compromisso", System.Data.DataRowVersion.Original, null));
                  this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_datahora_compromisso", System.Data.OleDb.OleDbType.DBDate, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "datahora_compromisso", System.Data.DataRowVersion.Original, null));
                  this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_datahora_compromisso1", System.Data.OleDb.OleDbType.DBDate, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "datahora_compromisso", System.Data.DataRowVersion.Original, null));
                  this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_desc_compromisso", System.Data.OleDb.OleDbType.VarWChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "desc_compromisso", System.Data.DataRowVersion.Original, null));
                  //
                  // oleDbInsertCommand1
                  //
                  this.oleDbInsertCommand1.CommandText = "INSERT INTO compromissos(datahora_compromisso, desc_compromisso) VALUES (?, ?)";
                  this.oleDbInsertCommand1.Connection = this.dbConn;
                  this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("datahora_compromisso", System.Data.OleDb.OleDbType.DBDate, 0, "datahora_compromisso"));
                  this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("desc_compromisso", System.Data.OleDb.OleDbType.VarWChar, 60, "desc_compromisso"));
                  //
                  // oleDbSelectCommand1
                  //
                  this.oleDbSelectCommand1.CommandText = "SELECT id_compromisso as ID, desc_compromisso as Descrição, datahora_compromisso " +
                        "as Data_e_Hora FROM compromissos";
                  this.oleDbSelectCommand1.Connection = this.dbConn;
                  //
                  // oleDbUpdateCommand1
                  //
                  this.oleDbUpdateCommand1.CommandText = "UPDATE compromissos SET datahora_compromisso = ?, desc_compromisso = ? WHERE (id_" +
                        "compromisso = ?) AND (datahora_compromisso = ? OR ? IS NULL AND datahora_comprom" +
                        "isso IS NULL) AND (desc_compromisso = ?)";
                  this.oleDbUpdateCommand1.Connection = this.dbConn;
                  this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("datahora_compromisso", System.Data.OleDb.OleDbType.DBDate, 0, "datahora_compromisso"));
                  this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("desc_compromisso", System.Data.OleDb.OleDbType.VarWChar, 60, "desc_compromisso"));
                  this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_id_compromisso", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "id_compromisso", System.Data.DataRowVersion.Original, null));
                  this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_datahora_compromisso", System.Data.OleDb.OleDbType.DBDate, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "datahora_compromisso", System.Data.DataRowVersion.Original, null));
                  this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_datahora_compromisso1", System.Data.OleDb.OleDbType.DBDate, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "datahora_compromisso", System.Data.DataRowVersion.Original, null));
                  this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_desc_compromisso", System.Data.OleDb.OleDbType.VarWChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "desc_compromisso", System.Data.DataRowVersion.Original, null));
                  //
                  // dataSet
                  //
                  this.dataSet.DataSetName = "dataSet";
                  this.dataSet.Locale = new System.Globalization.CultureInfo("en-US");
                  //
                  // dataGrid
                  //
                  this.dataGrid.DataMember = "";
                  this.dataGrid.DataSource = this.dataSet;
                  this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                  this.dataGrid.Location = new System.Drawing.Point(24, 80);
                  this.dataGrid.Name = "dataGrid";
                  this.dataGrid.Size = new System.Drawing.Size(576, 280);
                  this.dataGrid.TabIndex = 0;
                  this.dataGrid.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                                                                                                                                                 this.dgTableStyle});
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(136, 24);
                  this.button1.Name = "button1";
                  this.button1.TabIndex = 1;
                  this.button1.Text = "button1";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // dgTableStyle
                  //
                  this.dgTableStyle.DataGrid = this.dataGrid;
                  this.dgTableStyle.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                                                                                                                                                               this.dgTextBoxColumn0,
                                                                                                                                                               this.dgTextBoxColumn1,
                                                                                                                                                               this.dgTextBoxColumn2});
                  this.dgTableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                  this.dgTableStyle.MappingName = "";
                  this.dgTableStyle.PreferredColumnWidth = 100;
                  //
                  // dgTextBoxColumn0
                  //
                  this.dgTextBoxColumn0.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
                  this.dgTextBoxColumn0.Format = "";
                  this.dgTextBoxColumn0.FormatInfo = null;
                  this.dgTextBoxColumn0.MappingName = "";
                  this.dgTextBoxColumn0.Width = 75;
                  //
                  // dgTextBoxColumn1
                  //
                  this.dgTextBoxColumn1.Format = "";
                  this.dgTextBoxColumn1.FormatInfo = null;
                  this.dgTextBoxColumn1.MappingName = "";
                  //
                  // dgTextBoxColumn2
                  //
                  this.dgTextBoxColumn2.Format = "";
                  this.dgTextBoxColumn2.FormatInfo = null;
                  this.dgTextBoxColumn2.MappingName = "";
                  //
                  // FrmPrincipal
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(632, 373);
                  this.Controls.Add(this.button1);
                  this.Controls.Add(this.dataGrid);
                  this.Name = "FrmPrincipal";
                  this.Text = "Compromissos";
                  ((System.ComponentModel.ISupportInitialize)(this.dataSet)).EndInit();
                  ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new FrmPrincipal());
            }

            private void button1_Click(object sender, System.EventArgs e)
            {                  
                  dataGrid.TableStyles[0].GridColumnStyles[0].Width = 10;
                  dataGrid.TableStyles[0].GridColumnStyles[1].Width = 50;
                  dataGrid.TableStyles[0].GridColumnStyles[2].Width = 100;
                  dataGrid.Refresh();
            }
      }
}
LeoHyppolitoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ptmcompCommented:
You must set the mapping name of the TableStyle to the TableName of the DataTable you want to display. Also you need to set the ColumnStyle's MappingName to DataColumn.ColumnName
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeoHyppolitoAuthor Commented:

It worked...

Thanks!



0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.