如何在asp.net Web应用程序中将泛型类值添加到Gridview

问题描述:



我是使用asp.net进行Web应用程序开发的新手。我想在gridview中保存泛型类数据。我有包含几个控件和按钮的Web表单。当用户点击ADD按钮然后所有控件值在类中被占用时,则类值在gridview中被保存。我只有空的gridview结构。



Plz检查以下代码。



Repeater.cs

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

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



Default.aspx

<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



Default.aspx.cs

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;
        }







我只有空的griedview。

请帮我解决问题。



Thanx




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

Thanx

问题在于:

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





您使用'它em否'和'项目名称'用于定义标题的名称。但是在你的(非泛型)类中,你只有 INo IName 。这是什么意思?您必须定义要绑定的数据字段。请参阅: BoundField类 [ ^ ]



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[^]

<asp:BoundField HeaderText="Item No" datafield="INo"/>
<asp:BoundField HeaderText="Item Name" datafield="IName" />





不要忘记定义其他属性/字段。



Do not forget to define other properties/fields.