Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Dear sir,

I am retrieving data fro database and that data stored in datatable.

and i want to bind datatable data to dynamic gridview columns in the specied cell.

I am trying like this.

we are developping the project 3-tier architeture
dk = tbal.retriveheaderdata(clas, sec, k1, tim1);

for (int hj = 0; hj < dk.Rows.Count; hj++)
{
    TableCell tc = new TableCell();
    Label ll = new Label();
    ll.ID = f + hj.ToString();
    tc.Controls.Add(ll);
    tc.Text = dk.Rows[hj].ToString();


    GridView2.Rows[hj].Cells.Add(tc);


}
Posted
Updated 4-Aug-18 9:11am
v2

 
Share this answer
 
 
Share this answer
 
Sir,
Please check out my code. Please checkout at " tc.Text = dt.Rows[hj].ToString();";

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Edittime : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt1 = new DataTable();
        timetbal obj = new timetbal();
        if (!IsPostBack)
        {
            dt1 = obj.class1();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                DataRow dr = dt1.Rows[i];
                DropDownList1.Items.Add(dr[0].ToString());
            }
        }
    }

    string clas;
    string sec;
    ArrayList jk = new ArrayList();
    string f = "murali";
    string tim1;
    DataTable dk = new DataTable();
    BoundField bField = new BoundField();
    BoundField bField1 = new BoundField();
    BoundField bField2 = new BoundField();
    BoundField bField3 = new BoundField();
    static TimeSpan t3;
    static TimeSpan t4;
    static TimeSpan n6;
    static TimeSpan n8;
    string k1;
    protected void Button1_Click(object sender, EventArgs e)
    {
        timetbal tbal = new timetbal();
        DataTable dt = new DataTable();
        string class1 = DropDownList1.SelectedItem.ToString();
        string[] k = class1.Split('-');
        clas = k[0].ToString();
        sec = k[1].ToString();
        dt = tbal.retrivevalues(clas, sec);
        string start_time = dt.Rows[0][0].ToString();
        string period_time = dt.Rows[0][1].ToString();
        string no_period = dt.Rows[0][2].ToString();
        string break_time = dt.Rows[0][3].ToString();
        string lunch_time = dt.Rows[0][4].ToString();



        //Header Text Values

        GridView2.Width = 1500;
        int n = Convert.ToInt32(no_period);
        for (int i = 0; i < n; i++)
        {
            if (i == 0)
            {
                TimeSpan t1 = System.TimeSpan.Parse(start_time);
                TimeSpan t2 = System.TimeSpan.Parse(period_time);
                t3 = t1.Add(t2);
                string h = Convert.ToString(t1) + "-" + Convert.ToString(t3);
                GridView2.Visible = true;
                bField.HeaderText = h.ToString();
                GridView2.Columns.Add(bField);

            }
            else if (i < 2)
            {
                TimeSpan t1 = t3;
                TimeSpan t2 = System.TimeSpan.Parse(period_time);
                t3 = t1.Add(t2);
                Label6.Text = Convert.ToString(t1) + "-" + Convert.ToString(t3);
                GridView2.Visible = true;
                BoundField bField = new BoundField();
                bField.HeaderText = Label6.Text;
                GridView2.Columns.Add(bField);
            }
            else if (i == 2)
            {
                TimeSpan n1 = System.TimeSpan.Parse(break_time);
                t4 = t3.Add(n1);
                string k1 = Convert.ToString(t3) + "-" + Convert.ToString(t4);
                Label lbl = new Label();
                lbl.Text = k1.ToString();
                bField1.HeaderText = lbl.Text;
                GridView2.Columns.Add(bField1);
            }
            else if (i > 2 && i < 5)
            {
                TimeSpan t1 = t4;
                TimeSpan t2 = System.TimeSpan.Parse(period_time);
                t4 = t1.Add(t2);
                Label6.Text = Convert.ToString(t1) + "-" + Convert.ToString(t4);
                GridView2.Visible = true;
                BoundField bField = new BoundField();
                bField.HeaderText = Label6.Text;
                GridView2.Columns.Add(bField);
            }


            else if (i == 5)
            {
                TimeSpan n5 = System.TimeSpan.Parse(lunch_time);
                n6 = t4.Add(n5);
                string f = Convert.ToString(t4) + "-" + Convert.ToString(n6);
                Label lbl1 = new Label();
                lbl1.Text = f.ToString();
                bField2.HeaderText = lbl1.Text;
                GridView2.Columns.Add(bField2);
            }
            else if (i > 5 && i < 8)
            {
                TimeSpan t1 = n6;
                TimeSpan t2 = System.TimeSpan.Parse(period_time);
                n6 = t1.Add(t2);
                Label6.Text = Convert.ToString(t1) + "-" + Convert.ToString(n6);
                GridView2.Visible = true;
                BoundField bField = new BoundField();
                bField.HeaderText = Label6.Text;
                GridView2.Columns.Add(bField);
            }

            else if (i == 8)
            {
                TimeSpan n7 = System.TimeSpan.Parse(break_time);
                n8 = n6.Add(n7);
                string q = Convert.ToString(n6) + "-" + Convert.ToString(n8);

                bField3.HeaderText = q.ToString();
                GridView2.Columns.Add(bField3);

            }
            else
            {
                TimeSpan t11 = n8;
                TimeSpan t2 = System.TimeSpan.Parse(period_time);
                n8 = t11.Add(t2);
                string aa = Convert.ToString(t11) + "-" + Convert.ToString(n8);
                GridView2.Visible = true;
                BoundField bField = new BoundField();
                bField.HeaderText = aa.ToString();
                GridView2.Columns.Add(bField);
            }
        }
        GridView2.DataBind();

        for (int j = 1; j < GridView2.Columns.Count; j++)
        {
            tim1 = GridView2.Columns[j].HeaderText;
            dt = tbal.retriveheader(clas, sec, tim1);
           
            GridView2.DataSource = dt;
            GridView2.DataBind();
            k1 = dt.Rows[0][1].ToString();
          
            
            //dk = tbal.retriveheaderdata(clas, sec, k1, tim1);
            //for (int hj = 0; hj < dk.Rows.Count; hj++)
            //{
            //    DataRow dr = dk.Rows[hj];
            //    dk.Rows.Add(dr[0].ToString());
            //    //GridView2.DataSource = dk;
            //    // GridView2.DataBind();
            //}


            dk = tbal.retriveheaderdata(clas, sec, k1, tim1);
            for (int hj = 0; hj < dk.Rows.Count; hj++)
            {
                TableCell tc = new TableCell();
                Label ll = new Label();
                ll.ID = f + hj.ToString();
                tc.Controls.Add(ll);
                tc.Text = dt.Rows[hj].ToString();

                GridView2.Rows[hj].Cells.Add(tc);

            }



        }
    }
}
 
Share this answer
 
v3
Sir ,

I don't want add rows with gridview.
I want to bind data to dynamic gridview cell.
Look at my gridview .

I want to bind data at "06:00:00-07:00:00" in specified cell But It added to outside of gridview.

Even i remove autogenerate columns from gridview as i am getting output as below.



Week 06:00:00-07:00:00 07:00:00-08:00:00 08:00:00-08:10:00 System.Data.DataRow
Friday
Monday
Saturday
Sunday
Thursday
Tuesday
Wednesday
 
Share this answer
 
sir,

I used rowindex for merging gridview cells.I got output about my requirement, but my problem is i want to edit data in gridview cells.


I am using rowcommand i got value from 0th row. But remaining values get null values. So Please try to solve my problem.
this is my code check it once solve my problem

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            timetbal tbal = new timetbal();
            DataTable dk = new DataTable();

            dk = tbal.viewcount();

            int h = Convert.ToInt32(dk.Rows[0][1].ToString());

            if (e.Row.RowIndex == j)
            {
                e.Row.Cells[0].RowSpan = h;
                j = j + h;
            }
            else if (e.Row.RowIndex == j)
                e.Row.Cells[0].RowSpan = h;
            else
            {
                e.Row.Cells.Remove(e.Row.Cells[0]);
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName == "abc")
        {
            string s = e.CommandArgument.ToString();
            Session["murali"] = s.ToString();
            Response.Redirect("updatedata.aspx");
        }
    }
 
Share this answer
 
v3
Comments
Jibesh 28-Jan-13 19:28pm    
Please dont use the solution field to post your comments. use the 'Have a question or comment' widget to post your comments so that the author gets notification and he can reply to your query. If you post it as solution author may miss this and rare chance to look your query again.

Also if you want edit your question use 'Improve Question' link to modify your question
This Code is used to bind dynamic grid of selected record from a grid

private void BindDynaicGrd()
    {
        //instance of a datatable
        DataTable dt = new DataTable();
        //instance of a datarow
        DataRow drow;
        //creating two datacolums dc1 and dc2 
        DataColumn dc1 = new DataColumn("Code", typeof(string));
        DataColumn dc2 = new DataColumn("Name", typeof(string));
        //adding datacolumn to datatable
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);
       
        if (grd.Rows.Count > 0)
        {
            foreach (GridViewRow gvr in grdSites.Rows)
            {
               
                CheckBox chk_Single = (CheckBox)gvr.FindControl("chkSingle");
                if (chk_Single.Checked == true)
                {
                    Label lbl_Code = (Label)gvr.FindControl("lblCode");
                    Label lbl_Name = (Label)gvr.FindControl("lblName");
                    //instance of a datarow
                    drow = dt.NewRow();
                    //add rows to datatable
                    //add Column values
                    drow = dt.NewRow();
                    drow["Code"] = lbl_SiteCode.Text;
                    drow["Name"] = lbl_SiteName.Text.ToString();
                    dt.Rows.Add(drow);
                }
            }
        }
        //set gridView Datasource as dataTable dt.
        gridcl.DataSource = dt;
        //Bind Datasource to gridview
        gridcl.DataBind();
    }
 
Share this answer
 
v2
Comments
CHill60 2-Aug-18 6:47am    
2 days ago I posted a comment against one of your solutions ... "I'm struggling to understand why your solution is better than the accepted solution... perhaps if you add(ed) some words to explain the code it would help?"
Just dumping code against resolved questions is likely to attract downvotes and reports ... you can use the "Improve solution" link to add some explanation to your solution(s)
Richard Deeming 3-Aug-18 13:13pm    
FIVE AND A HALF YEARS too late.
https://www.aspsnippets.com/Articles/Dynamically-create-DataTable-and-bind-to-GridView-in-ASPNet.aspx

Try this
 
Share this answer
 
Comments
Richard Deeming 6-Aug-18 11:22am    
FIVE AND A HALF YEARS too late.

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