【问题标题】:How to edit dynamically generated datatable and datagridview?如何编辑动态生成的datatable和datagridview?
【发布时间】:2014-09-12 16:57:10
【问题描述】:

所以我有一个看起来像这样的表单:

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中的表是这样的:

SQL Table Image Link

所以当它运行时会显示:

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

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

【问题讨论】:

    标签: c# mysql datagridview datatable


    【解决方案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 信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2016-06-11
      • 2020-03-20
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多