Solved

How to change the column width in a dataGrid?

Posted on 2003-11-15
2
546 Views
Last Modified: 2010-04-16

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();
            }
      }
}
0
Comment
Question by:LeoHyppolito
2 Comments
 
LVL 10

Accepted Solution

by:
ptmcomp earned 50 total points
ID: 9758104
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
 

Author Comment

by:LeoHyppolito
ID: 9759827

It worked...

Thanks!



0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question