Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Does not fill the datagridview with this datatable, where is the problem?

this is the code

C#
private void button1_Click(object sender, EventArgs e)
{
     llenarDataSet();
}

public static DataSet llenarDataSet()
{
    DataTable dataT = new DataTable();
    dataT.Columns.Add("numeroactivo");
    dataT.Columns.Add("serieactivo");

    DataRow datarow;
    datarow = dataT.NewRow();
    datarow["numeroactivo"] = "0001";
    datarow["serieactivo"] = "339434111";
    ///Anadimos esta fila
    dataT.Rows.Add(datarow);

    datarow = dataT.NewRow();
    datarow["numeroactivo"] = "0002";
    datarow["serieactivo"] = "999991111";
    ///Anadimos esta fila
    dataT.Rows.Add(datarow);

    DataSet1 ds = new DataSet1();
    ds.Tables.Add(dataT);
    
    return ds;
}

private void button2_Click(object sender, EventArgs e)
{
    DataSet1 ds = new DataSet1();
    dataGridView1.DataSource = ds.Tables[0];

}
Posted
Updated 3-May-14 15:15pm
v2

1 solution

you need to use return dataset of your method

C#
private void button2_Click(object sender, EventArgs e)
{
   dataGridView1.DataSource = llenarDataSet().Tables[0];;
 
}


in your button1_Click, you call llenarDataSet but never used returned value of that method. you need to keep that value in form field or property, if you going to use it on another event. in your button2_Click what you are doing is creating new DataSet and assigning it as datasource. there is no data in new DataSet. you will get empty gridview at the end.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900