Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<pre lang="HTML">
i have a grid view.
grid view have three Columns.

Label | LabelValue | DefaultValue
I1 | A1 | 123
I2
I3
I4 | A2 | 456

i want to show data as above
when i store value in database i want to store data only with values LabelValue not blank
so only
I1 | A1 | 123
I4 | A2 | 456
gets stored in DB

so i want to show data in respective rows
how i can achieve this ..


ASP
<asp:GridView runat="server" ID="grdlabel" AutoGenerateColumns="false" OnRowDataBound="grdlabel_RowDataBound"
           DataKeyNames="label,labelName,labelValue">
           <Columns>
               <asp:TemplateField HeaderText="ILabel">
                   <ItemTemplate>
                       <asp:TextBox runat="server" ReadOnly="true" ID="txtlabel"></asp:TextBox>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="LabelName">
                   <ItemTemplate>
                       <asp:TextBox runat="server" ID="txtLabelName"></asp:TextBox>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="DefaultValue">
                   <ItemTemplate>
                       <asp:TextBox runat="server" ID="txtDefaultValue"></asp:TextBox>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>

<asp:Button ID="Button1" runat="server" Text="Save to DB" OnClick="Button1_Click" />
   <asp:Button ID="Button2" runat="server" Text="Bind Data From DB" OnClick="Button2_Click" />



C#
#region ... variable...
    List<data> lstData = null;
    List<data> lstDataToSave = null;
    #endregion

 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                lstData = new List<data>();
                for (int i = 1; i <= 10; i++)
                {
                    data oData = new data();
                    oData.label = "I" + i;
                    oData.labelName = "";
                    oData.labelValue = "";
                    lstData.Add(oData);
                    oData = null;
                }



                grdlabel.DataSource = lstData;
                grdlabel.DataBind();
            }
        }
        catch (Exception ex)
        {

        }
}
 protected void grdlabel_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txtLabel = e.Row.FindControl("txtlabel") as TextBox;
            txtLabel.Text = grdlabel.DataKeys[e.Row.RowIndex][0].ToString();

            TextBox txtLabelName = e.Row.FindControl("txtLabelName") as TextBox;
            txtLabelName.Text = grdlabel.DataKeys[e.Row.RowIndex][1].ToString();
            TextBox txtDefaultValue = e.Row.FindControl("txtDefaultValue") as TextBox;
            txtDefaultValue.Text = grdlabel.DataKeys[e.Row.RowIndex][2].ToString();

            if (e.Row.RowIndex == 7)
            {
                txtLabelName.Text = "FixName1";
                txtLabelName.ReadOnly = true;
                txtDefaultValue.Text = grdlabel.DataKeys[e.Row.RowIndex][2].ToString();

            }
            if (e.Row.RowIndex == 8)
            {
                txtLabelName.Text = "FixName2";
                txtLabelName.ReadOnly = true;
                txtDefaultValue.Text = grdlabel.DataKeys[e.Row.RowIndex][2].ToString();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lstDataToSave = new List<data>();
        try
        {
            foreach (GridViewRow row in grdlabel.Rows)
            {
                TextBox txtLabel = row.FindControl("txtlabel") as TextBox;
                TextBox txtLabelName = row.FindControl("txtLabelName") as TextBox;
                TextBox txtDefaultValue = row.FindControl("txtDefaultValue") as TextBox;
                if (txtLabelName.Text != "")
                {
                    data oData = new data();
                    oData.label = txtLabel.Text;
                    oData.labelName = txtLabelName.Text;
                    oData.labelValue = txtDefaultValue.Text;
                    lstDataToSave.Add(oData);
                }
            }


            Session["DataToSave"] = lstDataToSave;
        }
        catch (Exception ex)
        { }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        List<data> lstData = new List<data>();
        lstData = (List<data>)Session["DataToSave"];

        try
        {
            grdlabel.DataSource = lstData;
            grdlabel.DataBind();
        }
        catch (Exception ex)
        {

        }

    }


What I have tried:

i tried with a list
it show data like

I1 | A1 | 123
I4 | A2 | 456
I3
I4 | |
Posted
Updated 21-Aug-16 2:20am
v3
Comments
Karthik_Mahalingam 20-Aug-16 1:44am    
post the code
kedar001 21-Aug-16 8:21am    
thanks for reply..
please check the Code .
Karthik_Mahalingam 21-Aug-16 11:56am    
add all the conditions in below .
if (txtLabelName.Text != "")
kedar001 21-Aug-16 12:34pm    
No.
I need to store labels with label name only ,and when fetch from db show them at respective possition

1 solution

Make a new List out of your existing one and make sure that list only contains data that is populated, so something like

C#
var newList = list.Where(x => !string.IsNullOrWhiteSpace(x.SomeProperty)).ToList();


and make that the datasource of your grid.
 
Share this answer
 
Comments
Maciej Los 21-Aug-16 16:22pm    
5ed!

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