Sina M.Azad Sina M.Azad - 1 year ago 70
MySQL Question

Custom DataGridview Width when calling a MySql Table

I'm Trying to make a program that lists name of all the sub folders of a chosen folder, it all work but i cant seem to make a custom width for my datagridview , I've been looking for an answer for hours , but they mostly wont work, I tried :

Gata.columns[0].width = 100;
or stuff like this , but they don't work.

this did not work either : MSDN - DataGridViewColumn.Width Property

it seems that they are mainly used for unbound grids , that i have no idea about I tried to link my MySql table to one but failed again. It is pretty much the 2nd program i'm writing so please forgive my noobishness !

I want my table to look like the picture here.

the auto-size and fill statement on grids properties wont do the trick.
I've checked a lot of answer on stack-overflow but none answers this. Thanks in advance for the help !

here is the code im using :

String sqlcon = "datasource=localhost;port=3306;username=anime;password=anime";
MySqlConnection myanimedb0con = new MySqlConnection(sqlcon);
MySqlDataAdapter myanimedb0ada = new MySqlDataAdapter();
MySqlCommand myanimedb0cmd = new MySqlCommand("insert into anime.anime0list ( Anime_Name , Anime_Root ) values ( '" + MySql.Data.MySqlClient.MySqlHelper.EscapeString(dir.Name) + "' , '" + dir.Parent + "' );", myanimedb0con);
MySqlCommandBuilder myanimedb0cb = new MySqlCommandBuilder(myanimedb0ada);
myanimedb0ada.SelectCommand = myanimedb0cmd;
DataTable Gate = new DataTable();
BindingSource b0Gate = new BindingSource();
b0Gate.DataSource = Gate;
this.Gate.DataSource = b0Gate;

// Updating the table after adding an item

MySqlCommand myanimedb0cmd2 = new MySqlCommand("select * from anime.anime0list ;", myanimedb0con);

myanimedb0ada.SelectCommand = myanimedb0cmd2;

b0Gate.DataSource = Gate;
this.Gate.DataSource = b0Gate;

Answer Source

One way to do it is to set the column width one at a time like this if you are using the default windows forms or web controls:

private void Button5_Click(object sender, System.EventArgs e)
    DataGridViewColumn column = dataGridView.Columns[0];
    column.Width = 60;

You can also do it by column like this if you like:

for (int i = 0; i < DataGridView1.Columns.Count; i++)
    DataGridView1.Columns[i].Width = 30;

Or you can manipulate it within the event...

dataGridView1.ColumnAdded += new DataGridViewColumnEventHandler(dataGridView1_ColumnAdded);   
void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)   
    e.Column.Width = 30;   

This has to work, gauranteed. Please let me know if you come right?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download