Hikaros
Hikaros

Reputation: 121

How to edit dynamically generated datatable and datagridview?

So i have a form that looks like this:

enter image description here

Image Link

and I generate the datatable for the dataviewgrid in the load function:

private void loadEmpresas(){
    MySqlConnection myConn = new MySqlConnection(gVariables.myConnection);
    MySqlCommand command = new MySqlCommand("Select codempresa as 'Codigo', nomempresa as 'Nombre empresa' from contabilidad.empresas", myConn);

    try
    {
        MySqlDataAdapter sda = new MySqlDataAdapter();
        sda.SelectCommand = command;
        DataTable dbdataset = new DataTable();
        sda.Fill(dbdataset);
        BindingSource bSource = new BindingSource();

        bSource.DataSource = dbdataset;
        dataGridView1.DataSource = bSource;
        sda.Update(dbdataset);
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

the table in mysql looks like this:

enter image description here

SQL Table Image Link

so when it is running it shows this:

enter image description here

Now the problems that i have are that i have no idea how to modify the width of the columns, id like the entire table to cover that gray space and i want that on click it selects the entire row not just a cell. When it clicks the row i want to pupulate the rest of the textboxes but i have a problem with the click event which for testing purposes is this:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show("clicked");

    if (e.RowIndex >= 0)
    {
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];    
        MessageBox.Show(row.Cells[0].Value.ToString() + row.Cells[1].Value.ToString());                                
    }
    else
    {
        MessageBox.Show("wat");
    }                    
}

when I click it doesn't even show a messagebox sometimes, I seriously have no idea how to handle the click row event properly :C help please T_T

Upvotes: 0

Views: 1076

Answers (1)

ShawnOrr
ShawnOrr

Reputation: 1329

For updating the column widths try using DataGridViewColumn.width:

DataGridViewColumn column = dataGridView.Columns[0];
column.Width = 60;

DataGridViewColumn.Width property information.

To select the entire row you need to change the SelectionMode of the DataGrid to FullRowSelect:

this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.MultiSelect = false;

SelectionMode information.

Upvotes: 1

Related Questions