user3052918
user3052918

Reputation:

GridView doesn't show my datasource

I have gridview "gvData" ,i need to fetch the data of my function to gridview : (I am using c# application)

  algorithm.prison obj=new prison();
     gvData.DataSource=  obj.TitForTat();

After run i have a empty gridview. Here is my code:

 public DataSet TitForTat()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("myt");
        dt.Columns.Add(new DataColumn("iteration", typeof(int)));
        dt.Columns.Add(new DataColumn("prison1", typeof(string)));
        dt.Columns.Add(new DataColumn("prison2", typeof (string)));
        prison[] prisons = new prison[2];
        prisons[0] = new prison();
        prisons[1] = new prison();
        //---------------------------
        DataRow dr = dt.NewRow();
        prisons[0]._state = "c";
        prisons[1]._state = valueOfState[rd.Next(0, 1)];
        dr["iteration"] = 0;
        dr["prison1"] = "c";
        dr["prison2"] = prisons[1]._state;
        dt.Rows.Add(dr);
        //----------------------
        for (int i = 1; i <= _iteration; i++)
        {
            prisons[0]._state = prisons[1]._state;
            prisons[1]._state = valueOfState[rd.Next(0, 1)];
            DataRow dr1 = dt.NewRow();
            dr1["iteration"] =i;

            dr1["prison1"] = prisons[0]._state;
            dr1["prison2"] = prisons[1]._state;
            dt.Rows.Add(dr1);
        }

        ds.Tables.Add(dt);
        return ds;
    }

Upvotes: 0

Views: 175

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460028

If it's an ASP.NET-webforms GridView-control you need to call DataBind() after you have assigned the DataSource:

gvData.DataSource = obj.TitForTat();
gvData.DataBind();

If it's winforms you have to set the DataMember property if you use a DataSet instead of a DataTable:

gvData.DataSource = obj.TitForTat();
gvData.DataMember = "myt";  

... or use a DataTable in the first place:

DataSet ds = obj.TitForTat();
if(ds.Tables.Count > 0)
    gvData.DataSource = ds.Tables[0];

Upvotes: 1

Related Questions