c# - 如何编辑动态生成的datatable和datagridview?

标签 c# mysql datagridview datatable

所以我有一个如下所示的表单:

enter image description here

Image Link

我在加载函数中生成 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

最佳答案

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

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

DataGridViewColumn.Width属性(property)信息。

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

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

SelectionMode信息。

关于c# - 如何编辑动态生成的datatable和datagridview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24877769/

相关文章:

C# 只想在数据表中显示时间(不是日期)

c# - 无论如何提供一个 ics 日历文件,它将自动与更新保持同步

c# - 如何将 ICommand 添加到 FrameworkElement 中的事件?

c# - Winforms 中的 DatagridView,SqlDataReader 是否在循环结束时自动关闭?

C# datagridview 列到一个数组

mysql - 用户条目的数据库设计(使用mysql)

c# - 将复杂的结构(带有内部结构数组)从 C# 传递到 C++

c# - C# 中的 eBay API GetSellerList 调用返回错误 14005 'Web Service framework internal error. execute exception.'

python - mysql django 的 NULL 和 FALSE 是否相同?

PHP/MySQL - 如何使用 PHP 在网页上的任何位置显示 PHP 生成的文本?