c# - 在 Gridview 中选择特定列
问题描述
我有一个网格视图,其中显示了多条记录。在页面加载事件中,我正在从数据库中检索数据并填充 gridview。我还有另一列用于编辑和删除,它们是 Imagebuttons。现在我希望当用户单击编辑按钮时,然后基于 Id(用户单击时的该行),他/她重定向到具有所选 Id 的另一个网络表单,然后该人记录从数据库加载并填充相应的文本框。我没有更新 Gridview 中的记录。这是清晰理解的图像
这是我的 .aspx 代码
<asp:GridView ID="dgvEmployeesInformation" runat="server" CssClass=" table table-bordered table-hover table-responsive" AutoGenerateColumns="false" OnRowCommand="dgvEmployeesInformation_RowCommand">
<%--1st Column--%>
<Columns>
<asp:BoundField HeaderText="ID" DataField="Id" ShowHeader="false" Visible="false" ControlStyle-BackColor="#0066ff"/>
<asp:BoundField HeaderText="Name" DataField="Name"/>
<asp:BoundField HeaderText="Employee No" DataField="EmployeeNo"/>
<asp:BoundField HeaderText="Father Name" DataField="FatherName"/>
<asp:BoundField HeaderText="CNIC" DataField="CNIC"/>
<asp:BoundField HeaderText="Contact No" DataField="ContactNo"/>
<asp:BoundField HeaderText="City" DataField="City"/>
<asp:BoundField HeaderText="Post" DataField="RecommendedPost"/>
<asp:BoundField HeaderText="Status" DataField="Status"/>
<asp:BoundField HeaderText="Degree Status" DataField="DegreeStatus"/>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/edit.png" CommandName="Edit" Text='<%# Eval("ID") %>' ToolTip="Edit" Width="20px" Height="20px" runat="server"/>
<asp:ImageButton ImageUrl="~/Images/delete.png" CommandName="Delete" Text='<%# Eval("ID") %>' ToolTip="Delete" Width="20px" Height="20px" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是 .aspx.cs 代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dgvEmployeesInformation.DataSource = GetData();
dgvEmployeesInformation.DataBind();
}
}
private DataSet GetData()
{
DataSet DS = new DataSet();
using (SqlConnection con = new SqlConnection(Base.GetConnection))
{
SqlDataAdapter ad = new SqlDataAdapter("spSelectEmployeeSpecificRecord", con);
ad.SelectCommand.CommandType = CommandType.StoredProcedure;
ad.Fill(DS);
}
return DS;
}
protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
}
}
我不知道如何获取所选行的 ID 并将其发送到另一个页面。请帮忙
解决方案
e.CommandArgument
包含您单击它的索引。并且dgvEmployeesInformation.SelectedValue
包含您设置的 Id DataKeyNames
。
DataKeyNames
在您拥有的数据库字段上设置id
- 在您的情况下DataKeyNames="EmployeeNo"
,代码可以是
protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
int SelectedIndex;
if (int.TryParse(e.CommandArgument.ToString(), out SelectedIndex))
{
// make it selected after the click
dgvEmployeesInformation.SelectedIndex = SelectedIndex;
// now the SelectedValue contains the id
Edit(dgvEmployeesInformation.SelectedValue);
}
}
}
推荐阅读
- angular - 运行 ionic serve 时,项目没有通过 webpack 以及不同的输出颜色运行?
- php - PHP PDO:单个查询中的多个更新
- javascript - amCharts XY 图表 dateAxis 显示错误的日期
- c - if 语句中的编译时条件
- php - Laravel:语法错误或访问冲突:1055解决方案
- javascript - php代码总是与html代码混合吗?
- android - OnPreviewFrame(IntPtr data, Camera camera) 未被调用 | FastAndroid相机
- python - Python大整数性能
- javascript - 按字符串日期值对数组列表进行排序
- typescript - 像 SomeType 这样的类型是什么意思
在打字稿中?