所以我有一个如下所示的表单:
我在加载函数中生成 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中的表如下所示:
所以当它运行时它会显示:
现在我遇到的问题是我不知道如何修改列的宽度,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;
关于c# - 如何编辑动态生成的datatable和datagridview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24877769/