Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
recently I changed my GridView so it was able to add rows dynamically. The problem is, when I insert the data, I'm getting blank value at first.

I'm unable to get the details of my labels.text , but are able to get the text box value the first time. When the loop continues, I get 1 row of value from the label and 2 rows of value from the text box.


GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

    TableHeaderCell cell = new TableHeaderCell();
    cell.Text = "Details";
    cell.ColumnSpan = 3;
    row.Controls.Add(cell);


    cell = new TableHeaderCell();
    cell.Text = "Year 2018";
    cell.ColumnSpan = 4;
    row.Controls.Add(cell);

    cell = new TableHeaderCell();
    cell.ColumnSpan = 4;
    cell.Text = "Year 2019";
    row.Controls.Add(cell);

     cell = new TableHeaderCell();
    cell.Text = "Others";
    cell.ColumnSpan = 3;
    row.Controls.Add(cell);


What I have tried:

Label CenterId = (Label)row.FindControl("CenterId") as Label;
              string CenterId_no = Convert.ToString(CenterId.Text);


            TextBox txt_lastyearonDate = (TextBox)row.FindControl("txt_lastyearonDate") as TextBox;

            float py_ondte = float.Parse(txt_lastyearonDate.Text);
Posted
Updated 10-May-19 1:05am
Comments
Richard MacCutchan 10-May-19 6:28am    
Convert.ToString(CenterId.Text);

Why are you converting a text string to a text string?

Also, you have not explained which item is the one that has no value.
Anuragintit 10-May-19 7:06am    
Getting year on this, part, it shows input not in correct form, as it is blank for first time

Label lbl_lastyear_beforedate = (Label)row.FindControl("lbl_lastyear_beforedate") as Label;
float PY_Bdte = float.Parse(lbl_lastyear_beforedate.Text);
Richard MacCutchan 10-May-19 7:10am    
So you need to look at your data to see why it is not in the correct format. Unfortunately we are unable to see your screen from remote sites.
Anuragintit 10-May-19 7:26am    
<asp:gridview id="GvList" runat="server" autogeneratecolumns="False" onrowdatabound="GvList_RowDataBound" showfooter="True">
<columns>

<asp:templatefield headertext="Measurement Center Id">
<itemtemplate>

<asp:label id="CenterId" runat="server" text="<%#Eval("CenterId") %>" >


<headerstyle width="4%" horizontalalign="Center" cssclass="thead_cell">
<itemstyle horizontalalign="Center" width="4%">



<asp:templatefield headertext="Today Data">
<itemtemplate>

<asp:textbox id="txt_lastyearonDate_Rain" runat="server" text="<%#Eval("py_raintoday") %>" ondrop="return false;" onpaste="return false;"

autocomplete="off">


<controlstyle width="90%">
<headerstyle width="4%" horizontalalign="Center" cssclass="thead_cell">
<itemstyle horizontalalign="Center" width="4%">

<footertemplate>
<asp:label id="lbl_pytoday" runat="server" forecolor="Black" style="display:block; padding:0; text-align: center">



<asp:templatefield headertext="Select Type">
<itemtemplate>

<asp:dropdownlist id="ddl_type" runat="server">


<controlstyle width="100%">
<headerstyle width="3%" horizontalalign="Center" cssclass="thead_cell">
<itemstyle horizontalalign="Center" width="3%">




------------------------------------------------------------------------------------------------
Code Side

protected void GvList_DataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

TableHeaderCell cell = new TableHeaderCell();
cell.Text = "Details";
cell.ColumnSpan = 3;
row.Controls.Add(cell);


cell = new TableHeaderCell();
cell.Text = "Year 2018";
cell.ColumnSpan = 4;
row.Controls.Add(cell);

cell = new TableHeaderCell();
cell.ColumnSpan = 4;
cell.Text = "Year 2019";
row.Controls.Add(cell);

cell = new TableHeaderCell();
cell.Text = "Others";
cell.ColumnSpan = 4;
row.Controls.Add(cell);

// row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
GvList.HeaderRow.Parent.Controls.AddAt(0, row);
}

------------------------Submit click--------------------
foreach (GridViewRow row in GvList.Rows)

{
Label CenterId = (Label)row.FindControl("CenterId") as Label;
string CenterId_no = CenterId.Text;

DropDownList ddl = (DropDownList)row.FindControl("ddl_type") as DropDownList;
string dropdown_value = ddl.SelectedValue;

TextBox txt_lastyearonDate_Rain = (TextBox)row.FindControl("txt_lastyearonDate_Rain") as TextBox;

float py_ondte = float.Parse(txt_lastyearonDate_Rain.Text);

}

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

1 solution

You don't show us any control with any specific ID - so what makes you think that FindControl will return anything?
Try
C#
Label CentreID = row.Cells[1].Controls[0] as Label;

Or
C#
string s = (string) row.Cells[1].Value;
 
Share this answer
 

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