Click here to Skip to main content
15,881,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        makedatatable();
    }
    private void makedatatable()
    {
        DataTable dt = new DataTable("mytable");
        DataColumn col = new DataColumn();

        col = new DataColumn("sid");
        dt.Columns.Add(col);
        col = new DataColumn("sname");
        dt.Columns.Add(col);

        col = new DataColumn("dname");
        dt.Columns.Add(col);
        col = new DataColumn("date");
        dt.Columns.Add(col);
        col = new DataColumn("status");
        dt.Columns.Add(col);



        DataRow row;

        
            DataSet obj = staffdata.getattdstatus();

            
            
            if (obj.Tables[0].Rows.Count != 0)
            {
                for (int i = 0; i < obj.Tables[0].Rows.Count; i++)
                {

                    String stid = obj.Tables[0].Rows[i]["StaffId"].ToString();
                    row["sid"] = stid;
                    dt.Rows.Add(row);
                    String sname = obj.Tables[0].Rows[i]["Staffname"].ToString();
                    row["sname"] = sname;
                    dt.Rows.Add(row);

                    String dname = obj.Tables[0].Rows[i]["Deptname"].ToString();
                    row["dname"] = dname;
                    dt.Rows.Add(row);

  
                    String d = obj.Tables[0].Rows[i]["Date"].ToString();
                    row["date"] = d;
                    dt.Rows.Add(row);

                    String st = obj.Tables[0].Rows[i]["Status"].ToString();
                    row["status"] = st;
                    dt.Rows.Add(row);
               }

           }
                GridView1.DataSource = dt;
                GridView1.DataBind();
Posted
Updated 4-Mar-15 16:44pm
v3
Comments
DamithSL 4-Mar-15 22:41pm    
what is the error and which line you get the error?

why all these code!
change the aspx page Gridview markup to bind the specific columns as below
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="StaffId" HeaderText="sid" />
        <asp:BoundField DataField="Staffname" HeaderText="sname" />
        <asp:BoundField DataField="Deptname" HeaderText="dname" />
        <asp:BoundField DataField="Date" HeaderText="Date"  />
        <asp:BoundField DataField="Status" HeaderText="status"  />
    </Columns>
</asp:GridView>

then you can change the method as below
C#
private void makedatatable()
{
   DataSet obj = staffdata.getattdstatus();
   if(obj !=null && obj.Tables.Count >0)
   {

      GridView1.DataSource = obj.Tables[0];
      GridView1.DataBind();
   }
}
 
Share this answer
 
DataRow row=dt.NewRow();
if u do like this then u dont need to add columns to row like row["sid"]

simply assign the values to this row like row[0]="abc";



then finally dt.Rows.Add(row);
 
Share this answer
 
DamithSL is correct.

By the way for error, try this code. this is just for your understanding. remember variables in
C#
dt.Rows.Add()
must be in same sequence as columns are in table.



C#
for (int i = 0; i < obj.Tables[0].Rows.Count; i++)
{
    String stid = "id" + i;
    String sname = "Sname" + i;
    String dname = "Dname" + i;
    String d = "D" + i;
    String st = "St" + i;
    dt.Rows.Add(stid,sname,dname,d,st);

}
 
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