【问题标题】:Change tiny int in datagridview with checkbox使用复选框更改 datagridview 中的小 int
【发布时间】:2018-04-19 11:58:50
【问题描述】:

我正在使用 MySQL 数据库中的数据填充 DataGridView,其中一个值设置为 tiny int(布尔值),我希望它看起来像 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而不是真假。

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

【问题讨论】:

    标签: c# mysql visual-studio checkbox datagridview


    【解决方案1】:

    你可以在 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);
    }
    

    【讨论】:

      【解决方案2】:
      for (int i = 0; i < dataGridView1.RowCount; i++)
      {
      if(dataGridView1.Rows[i].Cells[7].Value != null)
      {
      this.dataGridView1.Rows[i].Cells[7] = new DataGridViewCheckBoxCell();
      }
      }
      

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

      【讨论】:

        猜你喜欢
        • 2012-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-22
        • 2014-10-18
        • 2013-10-28
        相关资源
        最近更新 更多