使用复选框更改datagridview中的tiny int

时间:2018-04-19 11:58:50

标签: c# mysql visual-studio checkbox datagridview

我用来自MySQL数据库的数据填充DataGridView,其中一个值设置为tiny int(boolean),我希望它看起来像DGV中的复选框而不是0和1,我该怎么做?

我在DGV中推送数据的代码。

MySqlConnection myConn = new MySqlConnection(conStr);
adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand(query, myConn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(adapter);
myConn.Open(); //da.Fill(ds, sTable); da.Update(ds, sTable); myConn.Close();

ds = new DataSet();
adapter.Fill(ds, sTable);
myConn.Close();

dataGridView1.Refresh();

dataGridView1.DataSource = ds;
dataGridView1.DataMember = sTable;
dataGridView1.Columns[0].Visible = false; 

我设法将我的单元格更改为如下所示的复选框:

for (int i = 0; i < dataGridView1.RowCount; i++)
{
this.dataGridView1.Rows[i].Cells[7] = new DataGridViewCheckBoxCell();
}

,但我认为它得到0和1而不是真和假并不高兴。

显然它按预期工作,但我收到错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以在msdn https://msdn.microsoft.com/ru-ru/library/system.windows.forms.datagridviewcheckboxcolumn(v=vs.110).aspx

上找到一个示例
private void AddOutOfOfficeColumn()
{
 DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
 {
     column.HeaderText = ColumnName.OutOfOffice.ToString();
     column.Name = ColumnName.OutOfOffice.ToString();
     column.AutoSizeMode = 
        DataGridViewAutoSizeColumnMode.DisplayedCells;
     column.FlatStyle = FlatStyle.Standard;
     column.ThreeState = true;
     column.CellTemplate = new DataGridViewCheckBoxCell();
     column.CellTemplate.Style.BackColor = Color.Beige;
 }

 DataGridView1.Columns.Insert(0, column);
}

答案 1 :(得分:0)

for (int i = 0; i < dataGridView1.RowCount; i++)
{
if(dataGridView1.Rows[i].Cells[7].Value != null)
{
this.dataGridView1.Rows[i].Cells[7] = new DataGridViewCheckBoxCell();
}
}

这很好地修复了它,显然程序显示错误,因为它认为它是空的。