locked
how to add value(string) in Label inside gridview during runtime c#/asp.net? RRS feed

  • Question

  • User-34107256 posted

    frnds,
    how to get value in the label inside gridview, during runtime. the value is in string variable.
    i referered sum article,,but i still doesnt understood the concept.pls help wid a piece of code..tx in adv.
    i know dat i have do in databound event..?
     
    wat i tried till nw..:
    i
    in grid databound event(object sender, GridViewRowEventArgs e)
    {  
    Label lbl1 = e.Row.Cells[1].FindControl(d1Date) as Label; //not wrking
    //dt1,dt2...so on are string value
    e.Row.Cells[1].Controls.Add(dt1) // creates only label in  cell but empty

    e.Row.Cells[2].Controls.Add(dt2) // creates only label in  cell but empty

    e.Row.Cells[3].Controls.Add(dt3) // creates only label in  cell but empty

    e.Row.Cells[4].Controls.Add(dt4) // creates only label in  cell but empty

    e.Row.Cells[5].Controls.Add(dt5) // creates only label in  cell but empty

    e.Row.Cells[6].Controls.Add(dt6) // creates only label in  cell but empty

    }
    i want the string value should be put in label inside gridview. in runtime?
    Also the value should change on dropdrownlist event..?

    Thursday, May 12, 2011 1:47 AM

Answers

  • User3866881 posted

    Hey, there are three main things that you should mention:

    1)If your GridView is fully Auto-generated (AutoGenerateColumns=True). You can just do that in Row_DataBound —— Just like _Manvel_ says:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       
    {
           
    if (e.Row.RowType == DataControlRowType.DataRow)
           
    {
               
    // Find the value in the c04_oprogrs column. You'll have to use

               
    string value = e.Row.Cells[0].Text;

               
    if (value == "1")
               
    {
                    e
    .Row.Cells[Index].Text = "Take";
               
    }
               
    else if (value == "2")
               
    {
                    e
    .Row.Cells[Index].Text = "Available";
               
    }
           
    }

       
    }

    2)If your AutoGeneratedColumns=False and you only have databound fiels in the GridView, try this:

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

                   Label l = new Label();

                   l.Text = xxx;

                     e.Row.Cells[Index].Controls.Add(l);
             }

    }

    3)If you are using ItemTemplates, you should say this:

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

    Label l = (Label)e.Row.FindControl("Id of Label");

    l.Text = xxx;


    }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 14, 2011 4:19 AM

All replies

  • User1697457068 posted
    try this

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                   
    Height="281px" Width="940px"
                   
    Font-Size="X-Small" AllowPaging="True"
                    onpageindexchanging
    ="GridView1_PageIndexChanging">
                   
    <Columns>
                       
    <asp:BoundField DataField="c04_oprogrs" HeaderText="Order Progress"
                           
    SortExpression="c04_oprogrs" />
                       
    <asp:BoundField DataField="c04_orderno" HeaderText="Order No."
                           
    SortExpression="c04_orderno" />
                       
    <asp:BoundField DataField="c04_orddate" HeaderText="Date of Order"
                           
    SortExpression="c04_orddate" DataFormatString="{0:d/MM/yyyy}" />
                       
    <asp:BoundField DataField="c04_ordval" HeaderText="Order Value"
                           
    SortExpression="c04_ordval" DataFormatString="{0:R#,###,###.00}" />
                       
    <asp:BoundField DataField="c04_delval" HeaderText="Delivered Value"
                           
    SortExpression="c04_delval" DataFormatString="{0:R#,###,###.00}" />
                       
    <asp:BoundField DataField="c04_invval" HeaderText="Invoice Value"
                           
    SortExpression="c04_invval" DataFormatString="{0:R#,###,###.00}" />
                       
    <asp:BoundField DataField="c04_orddesc" HeaderText="Order Description"
                           
    SortExpression="c04_orddesc" >
                           
    <ControlStyle Width="300px" />
                       
    </asp:BoundField>
                   
    </Columns>
               
    </asp:GridView>


    ===============================

    on page load

    SqlConnection myConnection;
           
    DataSet dataSet = new DataSet();
           
    SqlDataAdapter adapter;

           
    //making my connection
            myConnection
    = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMRASConnectionString"].ConnectionString);

            adapter
    = new SqlDataAdapter("Select TOP 40 c04_credno, c04_orderno, c04_orddate, c04_ordval, c04_delval, c04_invval, c04_oprogrs, c04_orddesc FROM C04ORDS WHERE c04_credno = '" + Session["CreditorNumber"] + "'AND c04_oprogrs <> 9 ORDER BY c04_orddate DESC", myConnection);

            adapter
    .Fill(dataSet, "MyData");

           
    GridView1.DataSource = dataSet;
           
    Session["DataSource"] = dataSet;
           
    GridView1.DataBind();


     

     

    -------------

     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       
    {
           
    if (e.Row.RowType == DataControlRowType.DataRow)
           
    {
               
    // Find the value in the c04_oprogrs column. You'll have to use

               
    string value = e.Row.Cells[0].Text;

               
    if (value == "1")
               
    {
                    e
    .Row.Cells[0].Text = "Take";
               
    }
               
    else if (value == "2")
               
    {
                    e
    .Row.Cells[0].Text = "Available";
               
    }
           
    }

       
    }


     

    Thursday, May 12, 2011 2:29 AM
  • User1697457068 posted
    try this

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                   
    Height="281px" Width="940px"
                   
    Font-Size="X-Small" AllowPaging="True"
                    onpageindexchanging
    ="GridView1_PageIndexChanging">
                   
    <Columns>
                       
    <asp:BoundField DataField="c04_oprogrs" HeaderText="Order Progress"
                           
    SortExpression="c04_oprogrs" />
                       
    <asp:BoundField DataField="c04_orderno" HeaderText="Order No."
                           
    SortExpression="c04_orderno" />
                       
    <asp:BoundField DataField="c04_orddate" HeaderText="Date of Order"
                           
    SortExpression="c04_orddate" DataFormatString="{0:d/MM/yyyy}" />
                       
    <asp:BoundField DataField="c04_ordval" HeaderText="Order Value"
                           
    SortExpression="c04_ordval" DataFormatString="{0:R#,###,###.00}" />
                       
    <asp:BoundField DataField="c04_delval" HeaderText="Delivered Value"
                           
    SortExpression="c04_delval" DataFormatString="{0:R#,###,###.00}" />
                       
    <asp:BoundField DataField="c04_invval" HeaderText="Invoice Value"
                           
    SortExpression="c04_invval" DataFormatString="{0:R#,###,###.00}" />
                       
    <asp:BoundField DataField="c04_orddesc" HeaderText="Order Description"
                           
    SortExpression="c04_orddesc" >
                           
    <ControlStyle Width="300px" />
                       
    </asp:BoundField>
                   
    </Columns>
               
    </asp:GridView>


    ===============================

    on page load

    SqlConnection myConnection;
           
    DataSet dataSet = new DataSet();
           
    SqlDataAdapter adapter;

           
    //making my connection
            myConnection
    = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMRASConnectionString"].ConnectionString);

            adapter
    = new SqlDataAdapter("Select TOP 40 c04_credno, c04_orderno, c04_orddate, c04_ordval, c04_delval, c04_invval, c04_oprogrs, c04_orddesc FROM C04ORDS WHERE c04_credno = '" + Session["CreditorNumber"] + "'AND c04_oprogrs <> 9 ORDER BY c04_orddate DESC", myConnection);

            adapter
    .Fill(dataSet, "MyData");

           
    GridView1.DataSource = dataSet;
           
    Session["DataSource"] = dataSet;
           
    GridView1.DataBind();


     

     

    -------------

     

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       
    {
           
    if (e.Row.RowType == DataControlRowType.DataRow)
           
    {
               
    // Find the value in the c04_oprogrs column. You'll have to use

               
    string value = e.Row.Cells[0].Text;

               
    if (value == "1")
               
    {
                    e
    .Row.Cells[0].Text = "Take";
               
    }
               
    else if (value == "2")
               
    {
                    e
    .Row.Cells[0].Text = "Available";
               
    }
           
    }

       
    }


     

    Thursday, May 12, 2011 2:29 AM
  • User-1423995609 posted

    Dude when you're using FindControl you should pass ID of control as parameter, so use quotation marks

    Label lbl1 = e.Row.Cells[1].FindControl("d1Date") as Label;

    Thou you can use FindControl with row, like this

    Label lbl1 = e.row.FindControl("d1Date") as Label;

    Check up this link, it has code, maybe that will help you to work out

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx

     

    Thursday, May 12, 2011 2:32 AM
  • User-34107256 posted

    _Mavel_,

    tx 4 reply, but i m still confused..

    do i need to use both the statement,?

    as i have added a lable in each cell (in design mode,inside column tag). but i fail to assign value from a string variable in code behind. in databound event.

     juss nw i cum to knw dat my string value becum null,as soon as it reaches the databound..no idea,y it is happening..?

    Thursday, May 12, 2011 6:36 AM
  • User-1423995609 posted

    No you don't have to use both statements, you can use only second statement, and when you're getting lbl1 simply assign dt1, dt2 ... and so on variable's to Text property, something like this

     

    Label lbl1 = e.Row.Cells[1].FindControl("d1Date") as Label;
    lbl1.Text = dt1;

    That's it, no more work should be done, don't add dt1 to cell's controls property, so in code that you've posted it would look like this

     

    in grid databound event(object sender, GridViewRowEventArgs e)
    {   
    Label lbl1 = e.Row.Cells[1].FindControl(d1Date) as Label; 
    lbl1.Text = dt1;
    }

    Thou one question, where did you get dt1, dt2.... string values??? You should understand that databound event takes each row, and makes operations upon them, so if you get dt1, dt2.... collection of string values, you should implement some logic, to make each string variable to be assigned to each row's Label's Text property. Ideally dt1, dt2... should be part of your datasource, so if you can't make things work post also where did u get dt1, dt2... collection fo string values.

    Thursday, May 12, 2011 7:53 AM
  • User3866881 posted

    Hey, there are three main things that you should mention:

    1)If your GridView is fully Auto-generated (AutoGenerateColumns=True). You can just do that in Row_DataBound —— Just like _Manvel_ says:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       
    {
           
    if (e.Row.RowType == DataControlRowType.DataRow)
           
    {
               
    // Find the value in the c04_oprogrs column. You'll have to use

               
    string value = e.Row.Cells[0].Text;

               
    if (value == "1")
               
    {
                    e
    .Row.Cells[Index].Text = "Take";
               
    }
               
    else if (value == "2")
               
    {
                    e
    .Row.Cells[Index].Text = "Available";
               
    }
           
    }

       
    }

    2)If your AutoGeneratedColumns=False and you only have databound fiels in the GridView, try this:

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

                   Label l = new Label();

                   l.Text = xxx;

                     e.Row.Cells[Index].Controls.Add(l);
             }

    }

    3)If you are using ItemTemplates, you should say this:

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

    Label l = (Label)e.Row.FindControl("Id of Label");

    l.Text = xxx;


    }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 14, 2011 4:19 AM