gpt4 book ai didi

c# - 文本框未显示在 GridView 中

转载 作者:行者123 更新时间:2023-11-30 17:02:00 24 4
gpt4 key购买 nike

我正在手动创建一个数据表以将其应用于 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;
}
}

最佳答案

为什么不在表单页面中使用 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>

所以你布置了你想要绑定(bind)的模板。上面的方法更灵活。

因此,当您将数据绑定(bind)到 GridView 时:

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

然后它将所有这些字段渲染到一个模板中。

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

关于c# - 文本框未显示在 GridView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20410800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com