0

在这里,我尝试了此代码。但是,我现在的问题是,它不显示任何数据。

这是我的代码

try
{
    DataTable dt = new DataTable();
    con.Open();

    dt.Load(new MySqlCommand("SELECT variant_name FROM tblVariant_Product WHERE product_name='" + cboProduct.Text + "'", con).ExecuteReader());

    DataColumn col = dt.Columns.Add(new DataColumn("Quantity", typeof(Int32));
    col.AllowDBNull = false;

    DataRow row = dt.NewRow();
    row["variant_name"] = "TOTAL";
    row["quantity"] = 0;
    dt.Rows.Add(row);

    dataGridView2.DataSource = dt;
    con.Close();
}
catch (Exception)
{
}
4

3 回答 3

3

写:

dt.AcceptChanges(); 

后:

dt.Rows.Add(row);
于 2013-05-15T05:58:13.897 回答
0

要添加列:

dt.Columns.Add(new DataColumn("ColumnName",Type.GetType("System.String")));

最好先删除它:

dataGridView2.DataSource = dt;
于 2013-05-15T04:16:24.003 回答
0
try{
    DataTable dt = new DataTable();
    con.Open();

    dt.Load(new MySqlCommand("SELECT variant_name FROM tblVariant_Product WHERE product_name='" + cboProduct.Text + "'", con).ExecuteReader());

    dt.Columns.Add(new DataColumn("Quantity", typeof(Int32));

    DataRow row = dt.NewRow();
    row["variant_name"] = "TOTAL";
    row["quantity"] = 0;
    dt.Rows.Add(row);

    dataGridView2.DataSource = dt;
    con.Close();
 }
 catch (Exception)
 {
 }
于 2013-05-15T07:22:04.753 回答