Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am new to web application developement using asp.net.I want save generic class data in gridview.I have web form including several controls and button . When user click on ADD button then all controls value stoared in class ,then class value is stoared in gridview.I got Only empty gridview structure.

Plz check the following code.

Repeater.cs
C#
public class RepeaterData
 {
     public int ino;
     public string iname;
     public string val1;
     public string val2;
     public string val3;
     public string val4;
     public string tot;
     public int INo
     {

         get { return ino;}
         set { ino = value; }
     }
     public string IName
     {
         get { return iname; }
         set { iname = value; }
     }
     public string Val1
     {
         get { return val1; }
         set { val1 = value; }
     }
     public string Val2
     {
         get { return val2; }
         set { val2 = value; }
     }
      public string Val3
     {
         get { return val3; }
         set { val3 = value; }
     }

      public string Val4
      {
          get { return val4; }
          set { val4 = value; }
      }
      public string Total
      {
          get { return tot; }
          set { tot = value; }
      }

 }


Default.aspx
HTML
<div style="border-style: none; border-color: inherit; border-width: 1px; height:174px; width:796px; padding-removed100PX">
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellSpacing="5" Width="787px">
         <Columns>
             <asp:BoundField HeaderText="Item No"/>
             <asp:BoundField HeaderText="Item Name" />
             <asp:BoundField HeaderText="Value1" />
             <asp:BoundField HeaderText="Value2" />
             <asp:BoundField HeaderText="Value3" />
             <asp:BoundField HeaderText="Value4" />
             <asp:BoundField HeaderText="Total" />
         </Columns>
        </asp:GridView>
    </div>


Default.aspx.cs
C#
List<RepeaterData> itemDetails = new List<RepeaterData>();

 protected void BtnAdd_Click(object sender, EventArgs e)
        {
          

            itemDetails = GetItemDetails();
            GridView1.DataSource = itemDetails;
            GridView1.DataBind();
            //TextBox2.Text = GridView1.Rows[0].Cells[1].Text;
        }
 protected List<RepeaterData> GetItemDetails()
        {
        
                    RepeaterData data = new RepeaterData();
                    data.INo = Convert.ToInt32(ItemNoList.Text);
                    data.IName = ItemNameList.Text;
                    data.Val1 = TxtVal1.Text;
                    data.Val2 = TxtVal2.Text;
                    data.Val3 = TxtVal3.Text;
                    data.Val4 = TxtVal4.Text;
                    data.Total = TxtTotal.Text;
                    //TextBox2.Text = data.Total;
           
                    itemDetails.Add(data);
              
            return itemDetails;
        }



I got only empty griedview.
Please help me to solve problem.

Thanx
Posted
Updated 31-Oct-15 10:14am
v2
Comments
But where did you provide the data to the properties and bind to the grid?
SujataJK 31-Oct-15 5:46am    
In Default.aspx.cs
like
List<repeaterdata> itemDetails = new List<repeaterdata>();

protected void BtnAdd_Click(object sender, EventArgs e)
{

itemDetails = GetItemDetails();
GridView1.DataSource = itemDetails;
GridView1.DataBind();

}

protected List<repeaterdata> GetItemDetails()
{
RepeaterData data = new RepeaterData();
data.INo = Convert.ToInt32(ItemNoList.Text);
data.IName = ItemNameList.Text;
data.Val1 = TxtVal1.Text;
data.Val2 = TxtVal2.Text;
data.Val3 = TxtVal3.Text;
data.Val4 = TxtVal4.Text;
data.Total = TxtTotal.Text;

itemDetails.Add(data);
con.Close();
return itemDetails;
}


}
SujataJK 31-Oct-15 5:48am    
List is declared like that
List<repeaterdata> itemDetails=new List<repeaterdata>();
Sergey Alexandrovich Kryukov 31-Oct-15 6:24am    
You don't have "generic class data". Any class instance is always an instance of a complete class, not generic.
Say, System.Collections.Generic.List<> is a generic type, but System.Collections.Generic.List<system.string> is a complete class, not generic. So, generics are unrelated to the question. Besides, you did not show us any.
—SA
SujataJK 31-Oct-15 7:00am    
thanx

1 solution

Problem is here:
XML
<asp:BoundField HeaderText="Item No"/>
<asp:BoundField HeaderText="Item Name" />


You use 'Item No' and 'Item Name' names to define headers. But in your (NOT-generic) class, you have only INo and IName. What it means? You have to define datafields to bind. See: BoundField Class[^]
XML
<asp:BoundField HeaderText="Item No" datafield="INo"/>
<asp:BoundField HeaderText="Item Name" datafield="IName" />


Do not forget to define other properties/fields.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Oct-15 20:15pm    
5ed.
—SA
Maciej Los 1-Nov-15 3:41am    
Thank you, Sergey.
Perfect 5. :)
Maciej Los 1-Nov-15 3:41am    
Thank you, Tadit.
SujataJK 5-Nov-15 4:05am    
Thanx @Maciej Los It's Really helpful for me.

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