gpt4 book ai didi

c# - 如何编辑所有行的整个列 asp :GridView

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:32 24 4
gpt4 key购买 nike

所以我用谷歌搜索了 stackoverflow,现在我的大脑重载了。我是 asp.net 的新手,但掌握了窍门。

我目前的要求是有一个 gridview,在加载时,所有行的 1 列立即进入编辑模式。我用这个问题和代码让我继续:

Allowing one column to be edited but not another

<asp:gridview id="CustomersGridView"      
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
autogenerateeditbutton="true"
allowpaging="true"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:boundfield datafield="CustomerID" readonly="true" headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName" readonly="true" headertext="Customer Name"/>
<asp:boundfield datafield="Address" headertext="Address"/>
<asp:boundfield datafield="City" headertext="City"/>
<asp:boundfield datafield="PostalCode" headertext="ZIP Code"/>
</columns>
</asp:gridview>

我已经搜索并找到了一些很好的解决方案,但是,我并不完全理解它们并且未能成功实现它们。它们是:

Edit/Update a single GridView field

Put multiple rows of a gridview into edit mode

所以我的问题是,您如何将所有行的列(例如邮政编码)同时置于编辑模式?

感谢所有帮助!

谢谢!

斯蒂芬

最佳答案

您将无法使用内置的编辑功能,但您可以通过使用 TemplateField 在编辑模式下加载列来实现这一点:

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" ...>    
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeColumn") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SomeOtherColumn" HeaderText="Foo" />
...
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Update" CommandArgument='<%# Container.ItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

代码隐藏:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = GridView1.Rows[(int)e.CommandArgument];
if (row != null)
{
TextBox txt = row.FindControl("TextBox1") as TextBox;
if (txt != null)
{
//get the value from the textbox
string value = txt.Text;
}
}
}

编辑:将按钮放在 GridView 之外,您将像这样更新:

<asp:GridView>
...
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />

代码隐藏:

protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
TextBox txt = row.FindControl("TextBox1") as TextBox;
if (txt != null)
{
//get the value from the textbox
string value = txt.Text;
}
}
}

关于c# - 如何编辑所有行的整个列 asp :GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8013647/

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