如何在WebForm中显示GridView记录 [英] How to Display GridView record in a WebForm
本文介绍了如何在WebForm中显示GridView记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i have a Grid View
<pre lang="HTML">
<asp:GridView AutoGenerateColumns="False" ID="GView" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="Horizontal" Width="100%"
OnRowEditing="GView_RowEditing">
<Columns>
<asp:BoundField DataField="UID" HeaderText="ID" Visible="false" />
<asp:TemplateField HeaderText="User Name" ItemStyle-CssClass="gv-item1">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UFName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UFName") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("UMName") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ULName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UNic" HeaderText="N.I.C" ItemStyle-CssClass="gv-item2"/>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
</pre>
i want to display the records showing in the gridview webform. but when ever i click over the edit button its always load first record. e.g i click over the edit button in print of 2nd record it will load the first record into the webform.
推荐答案
你做错了。
参考 - GridView.RowEditing事件 [ ^ ]。
You are doing wrong.
Refer - GridView.RowEditing Event[^].
protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
TaskGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
按照以下简单步骤:
Follow below simple steps:
-
从要显示gridview数据的网页上的工具箱中拖放GridView
Drag and drop a GridView from toolbox on Webpage where you wants to show the gridview data
添加按钮GridView显示数据的单击事件
Add a Button On which click event the GridView displays data
创建类似
SqLConnection con=new SqlConnection(ConfigurationManager.ConnectedStrings["NameOfYourConnectionString"].ToString());//for connection
- 现在转到按钮和代码的点击事件,如下所示:
con.open();
SqlCommand cmd=new SqlCommand("select * from YourTableName",con);
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
GridView.DataSource=ds;
GridView.DataBind();
con.close();
}
else
{
//code for error
}
这篇关于如何在WebForm中显示GridView记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文