如何编辑动态生成的数据表和datagridview?

时间:2014-07-22 02:03:44

标签: c# mysql datagridview datatable

所以我的表格看起来像这样:

enter image description here

Image Link

我在load函数中为dataviewgrid生成数据表:

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);
    }
}

mysql中的表格如下所示:

enter image description here

SQL Table Image Link

所以当它运行时会显示:

enter image description here

现在我遇到的问题是我不知道如何修改列的宽度,id就像整个表格一样覆盖那个灰色空间,我想要点击它选择整个行而不仅仅是一个单元格。当它点击该行时我想瞳孔化其余的文本框但我遇到了点击事件的问题,出于测试目的,这是:

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");
    }                    
}

当我点击它有时甚至不显示消息框时,我真的不知道如何正确处理点击行事件:C请帮助请T_T

1 个答案:

答案 0 :(得分:1)

要更新列宽,请尝试使用DataGridViewColumn.width:

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

DataGridViewColumn.Width财产信息。

要选择整行,需要将DataGrid的SelectionMode更改为FullRowSelect:

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

SelectionMode信息。

相关问题