0

Here I've tried this code. But, my problem now is that, it doesn't display any data.

Here is my code

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
  • dataGridView2.DataBind() ? Commented May 15, 2013 at 3:54
  • @DevProve after typing the datagridview2 and the period the databind does not show in the intellisense instead it shows DataBindings and DataBindingComplete. Commented May 15, 2013 at 3:56
  • @DevProve is there another way to add a new row? Commented May 15, 2013 at 4:05
  • opss sorry yours is window app Commented May 15, 2013 at 4:05

3 Answers 3

3

Write:

dt.AcceptChanges(); 

after:

dt.Rows.Add(row);
Sign up to request clarification or add additional context in comments.

11 Comments

The same, and I double check in my database table tblVariant_Product that it has data but when I run the program it doesn't display that data. Why is that?
Please check dt in QuickWatch at this dt.Load(new MySqlCommand("SELECT variant_name FROM tblVariant_Product WHERE product_name='" + cboProduct.Text + "'", con).ExecuteReader()); at debug time is datatable(dt) contains data at this point?
Sorry I'm a beginner in using visual studio, so how would I know that dt contains data?
add debug point using F9 key at this line and run the application
Okay I'm already running the application and the line is highlighted with the cursor blinking and after that?
|
0

To add column:

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

and its better to remove the it first:

dataGridView2.DataSource = dt;

6 Comments

No it has two columns variant_name and Quantity, whereas the Quantity is the one that I've added in datagridview2 property.
Okay I get it...I think I know the error. So now I have a question if you don't mind, what is the syntax for adding a column in datatable?
It doesn't display any data, anyway I updated my question already.
Check your cboProduct.Text it maybe null
I don't think so because it's functioning previously.
|
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)
 {
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.