ASP.NET - How to populate item template from datatable into gridview

Asked By bhanupratap singh on 18-Oct-12 03:54 AM
I have multi entry form in asp.net c#. First I fetch data from database into datatable. Then I want to populate datatable record into item template inside gridview. Actualy I am checking first data into database if data exist into database then fetch this data into datatable (dt). If dt has two record. then data should be populated inside gridview. 
Danasegarane Arunachalam replied to bhanupratap singh on 18-Oct-12 04:45 AM
You need to handle the Item_databound event, there find the control id using e.row.findcontrol method then bind the values

Here are the example

http://msdn.microsoft.com/en-us/library/aa479353.aspx
And

http://msdn.microsoft.com/en-us/library/bb288032.aspx
bhanupratap singh replied to Danasegarane Arunachalam on 18-Oct-12 04:51 AM
thanks for reply
But I have ItemTemplate inside Gridview not databound field ok.
So. First I check data into table if exist then I fetch data into dt1 (datatable). From this dt1 I want to populate data into gridview
Danasegarane Arunachalam replied to bhanupratap singh on 18-Oct-12 05:04 AM
Did you checked this link?

http://msdn.microsoft.com/en-us/library/aa479353.aspx

<Columns>
        <asp:BoundField HeaderText="Last" DataField="LastName" 
           SortExpression="LastName"></asp:BoundField>
        <asp:BoundField HeaderText="First" DataField="FirstName" 
           SortExpression="FirstName"></asp:BoundField>
        <asp:BoundField HeaderText="Hire Date" DataField="HireDate" 
           SortExpression="HireDate"
            DataFormatString="{0:d}"></asp:BoundField>
        <asp:TemplateField HeaderText="Seniority">
            <ItemTemplate>
                <%# ComputeSeniorityLevel(DateTime.Now – 
                     (DateTime)Eval("HireDate")) %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

Check the highlighted area, there HireDate is bounded using database only
bhanupratap singh replied to Danasegarane Arunachalam on 18-Oct-12 07:59 AM
Pls see my code
It bind data into gridview but same record in all rows why?...

protected void GrvMfdetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {

DropDownList ddlGrvFromStn = (DropDownList)e.Row.Cells[0].FindControl("ddlGrvFromStn");
            DropDownList ddlGrvTooStn = (DropDownList)e.Row.Cells[4].FindControl("ddlGrvTooStn");
            BLL bll = new BLL();
            ddlGrvFromStn.DataSource = bll.GettingStation();
            ddlGrvFromStn.DataValueField = "CODE_NAME".ToString();
            ddlGrvFromStn.DataTextField = "CODE_NAME".ToString();
            ddlGrvFromStn.DataBind();
            ddlGrvTooStn.DataSource = bll.GettingStation2();
            ddlGrvTooStn.DataValueField = "CODE_NAME".ToString();
            ddlGrvTooStn.DataTextField = "CODE_NAME".ToString();
            ddlGrvTooStn.DataBind();
DataTable dt = (DataTable)ViewState["Myview"];
                                if (dt.Rows.Count > 0)
                                {

for(int i=0;i<dt.count:i++)
{

 var ddlGrvFromStnn = (DropDownList)e.Row.FindControl("ddlGrvFromStn");
                            var txtcnmt = (TextBox)e.Row.FindControl("txtCnmt");
                            var txtPkg = (TextBox)e.Row.FindControl("txtPkg");
                            var txtweight = (TextBox)e.Row.FindControl("txtweight");
                            var ddlGrvTooStnn = (DropDownList)e.Row.FindControl("ddlGrvTooStn");
                            var txtFreight = (TextBox)e.Row.FindControl("txtFreight");
                            var txtGoods = (TextBox)e.Row.FindControl("txtGoods");
                            var txtPM = (TextBox)e.Row.FindControl("txtPM");
                            var txt12 = (TextBox)e.Row.FindControl("txt12");
}
}
}
Danasegarane Arunachalam replied to bhanupratap singh on 18-Oct-12 08:18 AM
This is wrong method of binding.

When you call the Gridview.Bind Method, then the gridview will be loaded with values one by one.

GrvMfdetails_RowDataBound will be fired for each rows in the DataTable dt. And also you have check either it is header row , datarow or footer row.

Something like

protected void GrvMfdetails_RowDataBound(object sender, GridViewRowEventArgs e)
      {

       if(e.Row.RowType == DataControlRowType.DataRow)
       {

        DropDownList ddlGrvFromStn = (DropDownList)e.Row.Cells[0].FindControl("ddlGrvFromStn");
        DropDownList ddlGrvTooStn = (DropDownList)e.Row.Cells[4].FindControl("ddlGrvTooStn");
        BLL bll = new BLL();
        ddlGrvFromStn.DataSource = bll.GettingStation();
        ddlGrvFromStn.DataValueField = "CODE_NAME".ToString();
        ddlGrvFromStn.DataTextField = "CODE_NAME".ToString();
        ddlGrvFromStn.DataBind();
        ddlGrvTooStn.DataSource = bll.GettingStation2();
        ddlGrvTooStn.DataValueField = "CODE_NAME".ToString();
        ddlGrvTooStn.DataTextField = "CODE_NAME".ToString();
        ddlGrvTooStn.DataBind();

       }

     }

bhanupratap singh replied to Danasegarane Arunachalam on 19-Oct-12 01:34 AM
Thanks for reply . it still place same record into gridview in all rows. pls help me out
thanks again
pls see below code where i m making mistake.....

protected void txtmfno_TextChanged(object sender, EventArgs e)
        {
   DataTable dt= (DataTable)ViewState["Myview"];
            dt = ds1.Tables[0];
            ViewState["Myview"] = dt;
            GrvMfdetails.DataSource = dt;
            GrvMfdetails.DataBind();
}

protected void GrvMfdetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {            

            if (e.Row.RowType == DataControlRowType.DataRow )//&& dt.Rows.Count>0)
            {
                 
                        DropDownList ddlGrvTooStn = (DropDownList)e.Row.Cells[4].FindControl("ddlGrvTooStn");

                        DropDownList ddlGrvFromStn = (DropDownList)e.Row.Cells[0].FindControl("ddlGrvFromStn");
                        BLL bll = new BLL();
                        ddlGrvFromStn.DataSource = bll.GettingStation();
                        ddlGrvFromStn.DataValueField = "CODE_NAME".ToString();
                        ddlGrvFromStn.DataTextField = "CODE_NAME".ToString();
                        ddlGrvFromStn.DataBind();
                        ddlGrvTooStn.DataSource = bll.GettingStation2();
                        ddlGrvTooStn.DataValueField = "CODE_NAME".ToString();
                        ddlGrvTooStn.DataTextField = "CODE_NAME".ToString();
                        ddlGrvTooStn.DataBind();
                        DataTable dt = (DataTable)ViewState["Myview"];

                        var ddlGrvFromStnn = (DropDownList)e.Row.FindControl("ddlGrvFromStn");
                        var txtcnmt = (TextBox)e.Row.FindControl("txtCnmt");
                        var txtPkg = (TextBox)e.Row.FindControl("txtPkg");
                        var txtweight = (TextBox)e.Row.FindControl("txtweight");
                        var ddlGrvTooStnn = (DropDownList)e.Row.FindControl("ddlGrvTooStn");
                        var txtFreight = (TextBox)e.Row.FindControl("txtFreight");
                        var txtGoods = (TextBox)e.Row.FindControl("txtGoods");
                        var txtPM = (TextBox)e.Row.FindControl("txtPM");
                        var txt12 = (TextBox)e.Row.FindControl("txt12");
                        
                        ddlGrvFromStnn.SelectedIndex = ddlGrvFromStnn.Items.IndexOf(ddlGrvFromStnn.Items.FindByValue(dt.Rows[0][0].ToString()));
                        txtcnmt.Text = dt.Rows[0][1].ToString();
                
                        txtPkg.Text = dt.Rows[0][2].ToString();
                        txtweight.Text = dt.Rows[0][3].ToString();
                        ddlGrvTooStnn.SelectedIndex = ddlGrvTooStnn.Items.IndexOf(ddlGrvTooStnn.Items.FindByValue(dt.Rows[0][4].ToString()));
                        txtFreight.Text = dt.Rows[0][5].ToString();
                        txtGoods.Text = dt.Rows[0][6].ToString();
                        txtPM.Text = dt.Rows[0][7].ToString();
                        txt12.Text = dt.Rows[0][8].ToString();
            }
        }
Danasegarane Arunachalam replied to bhanupratap singh on 19-Oct-12 01:44 AM
Pls post a screen shot. Code looks correct to me
bhanupratap singh replied to Danasegarane Arunachalam on 19-Oct-12 04:18 AM
From Station Cnmt No Pkg Weight Too Station Freight Description Pvt Marka Stn
                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                                                       
Total:              

bhanupratap singh replied to Danasegarane Arunachalam on 19-Oct-12 04:19 AM
From Station Cnmt No Pkg Weight Too Station Freight Description Pvt Marka Stn
                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                                                       
Total:              
Danasegarane Arunachalam replied to bhanupratap singh on 19-Oct-12 04:23 AM
This looks like correct for me. What do you say wrong here.

Pls post a small screen shot and explain.

And also debug the code add see the values here

protected void GrvMfdetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {            

            if (e.Row.RowType == DataControlRowType.DataRow )//&& dt.Rows.Count>0)
            {
bhanupratap singh replied to Danasegarane Arunachalam on 19-Oct-12 05:06 AM
It bind same record in all rows of gridivew. there are two record in dt 
like :

fromStation      CnmtNo     Weight     Goods

Delhi 1505 200 Computer Parts/// this row 1 
Mumbai 1506 300 Books .//// this row no is 2

I should get those two record in gridview same way
but it show row no 2 in to gridview. like:

Mumbai 1506 300 Books/// it should be first row data Problem is here
Mumbai 1506 300 Books
Danasegarane Arunachalam replied to bhanupratap singh on 19-Oct-12 05:21 AM
I don't find any issue with code. Comment all the code in the Row_databound method and enable line by line and see the results