文本框未显示在GridView中

时间:2013-12-05 21:11:19

标签: c# asp.net

我手动创建数据表以将其应用于gridview,并且文本框列未显示。其他列工作得很好,但我需要创建一个带有可输入文本框的列。

干杯, 克里斯

protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    int numPrizes = GetNumPrizes();//if there are voided prizes.

                    DataTable dt = new DataTable();
                    gvTournamentData.Columns.Clear();

                    //PrizeNumber
                    //Add the columns to the grid view
                    BoundField PrizeNumberBF = new BoundField();
                    PrizeNumberBF.DataField = "PrizeNumber";
                    PrizeNumberBF.SortExpression = "PrizeNumber";
                    PrizeNumberBF.HeaderText = "";
                    gvTournamentData.Columns.Add(PrizeNumberBF);
                    dt.Columns.Add("PrizeNumber");

                    //Place
                    //Add the columns to the grid view
                    BoundField PlaceBF = new BoundField();
                    PlaceBF.DataField = "Place";
                    PlaceBF.SortExpression = "Place";
                    PlaceBF.HeaderText = "Place";
                    gvTournamentData.Columns.Add(PlaceBF);
                    dt.Columns.Add("Place", typeof(String));
                    DataTable dtPlaces = GetPlaces(1043); //hard coded for now

                    ////PrizeName //BFOP Prize
                    //Add the columns to the grid view
                    BoundField PrizeNameBF = new BoundField();
                    PrizeNameBF.DataField = "BFOP Prize";
                    PrizeNameBF.SortExpression = "BFOP Prize";
                    PrizeNameBF.HeaderText = "BFOP Prize";
                    gvTournamentData.Columns.Add(PrizeNameBF);
                    dt.Columns.Add("BFOP Prize", typeof(String));
                    DataTable dtPrizeNames = GetPrizeNames(1043); //hard coded for now

                    ////NickName
                    dt.Columns.Add("NickName", typeof(TextBox));

                    numPrizes = 7; //hard coded for now
                    for (int index = 1; index <= numPrizes; index++)
                    {
                        // Start at Prize one instead of Prize zero
                        string ID = index.ToString();
                        DataRow row = dt.NewRow();
                        row["PrizeNumber"] = ID + ")";
                        row["Place"] = dtPlaces.Rows[index - 1]["Place"];
                        row["BFOP Prize"] = dtPrizeNames.Rows[index - 1]["PrizeName"];
                        //TextBox nicknameTB = new TextBox();
                        //nicknameTB.Text = ID + ")";
                        //row["NickName"] = nicknameTB;
                        /* for (int x = 0; x < numCols; x++)
                         {
                             PopulatePrizeNameLabel(ID);
                             PopulateAddButton(ID);
                         }*/
                        dt.Rows.Add(row);
                    }

                    gvTournamentData.DataSource = dt;
                    gvTournamentData.DataBind();
                    gvTournamentData.Visible = true;
                }
    }

1 个答案:

答案 0 :(得分:2)

为什么不在表单页面中使用TemplateColumn

示例:

<asp:GridView>
<Columns>
  <asp:TemplateColumn HeaderText="Place">
     <ItemTemplate>
       <%#Eval("Place")%>
     </ItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="NickName">
      <ItemTemplate>         
         <asp:TextBox ID="txt_NickName" runat="server" Width="85px"                 Text='<%#Eval("NickName")%>'>
                                    </asp:TextBox>
                    </ItemTemplate>
      </ItemTemplate>
   </asp:TemplateColumn>
</Columns>
</asp:GridView>

所以你要布置你想要绑定的模板。上述方法更灵活。

因此,当您将数据绑定到网格视图时:

gvTournamentData.DataSource = dt;
gvTournamentData.DataBind();

然后它会将所有这些字段呈现为模板。

使用此链接作为参考:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview%28v=vs.110%29.aspx