如何 select 网格视图中的特定单元格值
How To select Particular Cell Value in Grid View
我想要 select 特定的单元格值并将用于绑定控件但是我无法 select 单元格值
它没有检索数据
ASPX 代码
<asp:GridView ID="grdLogedUserDetails" OnRowDeleting="grdLogedUserDetails_RowDeleting" OnRowDataBound="grdLogedUserDetails_RowDataBound"
runat="server" Style="width: 100%; text-align: center" OnRowCommand="grdLogedUserDetails_RowCommand"
class="table table-striped table-bordered" AutoGenerateColumns="false" datakeynames="Ref_ID">
<Columns>
<asp:TemplateField HeaderText="Process No">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Ref_ID") %>' ID="lblRef_ID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("CompanyName") %>' ID="lblCompanyName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DivisionName") %>' ID="lblDivisionName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgDel" runat="server" ImageUrl="~/Images/delete.png" AlternateText="Delete" CommandName="Delete" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:ImageButton ID="imgSel" runat="server" ImageUrl="~/Images/edit-icon.png" AlternateText="Select" CommandName="Select" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
C#
protected void grdLogedUserDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow row = grdLogedUserDetails.Rows[rowIndex];
//Access Cell values.
int RefID= int.Parse(row.Cells[0].Text);
string Company= row.Cells[1].Text;
string Division= row.Cells[2].Text;
}
protected void grdLogedUserDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Access Cell values.
var RefId = int.Parse(e.Row.Cells[0].Text); // Error Input string is not in correct Formate
string Comany= e.Row.Cells[1].Text;
string Division= e.Row.Cells[2].Text;
}
}
调试时我可以看到如下错误:
“输入字符串的格式不正确”我哪里做错了
我的Table结构
________________________________________
Ref_ID| CompanyName | DivisionName |
______|_______________|_________________|
6 | Company | Sales |
8 | Company | Sales |
14 | Company | Sales |
______|_______________|_________________|
RefID 我把它当作“int”,CompanyName 和 DivisionName “string”所以我输入的字符串格式错误,
我哪里做错了请给我建议
问题是单元格不包含任何文本,而是一个标签 lblRef_ID
。所以你需要访问标签,而不是单元格值。
var label = e.Row.FindControl("lblRef_ID") as Label;
var RefId = int.Parse(label.Text);
除非您有 Command="Select" 但您确实有 Command="Select",否则 rowcommand 事件不会触发 rowindexed 更改。但是,您没有 CommandArugment。
但是,既然您有 command=Select,那么请将您的代码移至 SelectedIndexChanged。问题是您不能 get/grab 来自“行”命令事件的行,因为该行没有更改。 rowcommand 事件在 SelectedIndexChanged 之前触发。如前所述,除非您使用特殊命令(删除或 select),否则 SelectedIndexChanged 不会触发。
所以行索引不会在行命令事件中设置。但是,这意味着对于您的 select 按钮(和删除),您的代码将(可以)在 rowindexchanged 中,“如果”您对命令使用 say select(或“删除”)。如果您使用自定义命令名称,则不会触发 rowindexedchanged。
您可以继续使用 rowcommand,但您需要 CommandArugment 来传递行的 PK 或行的索引。
所以你在这里有两个选择。 (实际上是 3 个)。
您可以设置该按钮的CommandArugment 值。
要么去PK,要么就用这个
CommandArgument ='<%# Container.DataItemIndex%>'
我注意到在你的命令设置中,你没有设置 CommandArugment - 你必须在你的标记中设置它。上面的表达式将 return 索引放入网格中。
您可以将代码移动到 rowindexchanged。
不过,还有第三种选择。您可以在行命令中获取行索引。所以从地狱的深处,你可以使用这个惊人的疯狂声明:
If e.CommandName = "MySelect" Then
' now get row.
Dim gvRow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Debug.Print("row index = " & gvRow.DataItemIndex)
Dim MyLabel As Label = gvRow.FindControl("HotelName")
Debug.Print("Value ref id = " & MyLabel.Text)
End If
注意非常小心:
对于模板化字段 - 我们使用查找控件。对于非模板字段,则可以按单元格引用。
现在超级获奖丑陋的方式来获取行中的行命令?
在 C# 中它看起来像这样:
{
GridViewRow gvRow = (GridViewRow)(Control)e.CommandSource.NamingContainer;
}
因此,您必须使用上面的方法,或者如前所述,将您的代码移至 SelectedIndexChanged。
既然你只有一个 select 和删除按钮,那么我建议 SelectedIndexChanged。但是,如果您要说引入另一个按钮 - 那么您将不想使用 Select 或 Delete,而是您选择的自定义命令名称(比如 CommandName="MyViewer")。在这种情况下,SelectedIndexChanged 将不会触发。但随后您将有办法确定单击了哪个按钮(超出 select 和删除命令的需要)。
So in summary:
You have about 3+ ways to do this.
Keep in mind that in row command, the selected index event has NOT yet fired.
Keep in mind that if you use a custom row command name, then selected index WILL NOT fire.
Keep in mind that you can pick up the selected row with that award winning ugly line of code.
Keep in mind that you can't use cells() collection for your custom templated columns - so use find control.
Keep in mind that selectedindex in row command event has NOT changed nor fired yet.
由于您正在使用 select,因此您可以选择行命令事件或 selectedindex 更改事件 - 它们都会在您的情况下触发。
我想要 select 特定的单元格值并将用于绑定控件但是我无法 select 单元格值 它没有检索数据
ASPX 代码
<asp:GridView ID="grdLogedUserDetails" OnRowDeleting="grdLogedUserDetails_RowDeleting" OnRowDataBound="grdLogedUserDetails_RowDataBound"
runat="server" Style="width: 100%; text-align: center" OnRowCommand="grdLogedUserDetails_RowCommand"
class="table table-striped table-bordered" AutoGenerateColumns="false" datakeynames="Ref_ID">
<Columns>
<asp:TemplateField HeaderText="Process No">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Ref_ID") %>' ID="lblRef_ID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("CompanyName") %>' ID="lblCompanyName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DivisionName") %>' ID="lblDivisionName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgDel" runat="server" ImageUrl="~/Images/delete.png" AlternateText="Delete" CommandName="Delete" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:ImageButton ID="imgSel" runat="server" ImageUrl="~/Images/edit-icon.png" AlternateText="Select" CommandName="Select" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
C#
protected void grdLogedUserDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow row = grdLogedUserDetails.Rows[rowIndex];
//Access Cell values.
int RefID= int.Parse(row.Cells[0].Text);
string Company= row.Cells[1].Text;
string Division= row.Cells[2].Text;
}
protected void grdLogedUserDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Access Cell values.
var RefId = int.Parse(e.Row.Cells[0].Text); // Error Input string is not in correct Formate
string Comany= e.Row.Cells[1].Text;
string Division= e.Row.Cells[2].Text;
}
}
调试时我可以看到如下错误:
“输入字符串的格式不正确”我哪里做错了
我的Table结构
________________________________________
Ref_ID| CompanyName | DivisionName |
______|_______________|_________________|
6 | Company | Sales |
8 | Company | Sales |
14 | Company | Sales |
______|_______________|_________________|
RefID 我把它当作“int”,CompanyName 和 DivisionName “string”所以我输入的字符串格式错误,
我哪里做错了请给我建议
问题是单元格不包含任何文本,而是一个标签 lblRef_ID
。所以你需要访问标签,而不是单元格值。
var label = e.Row.FindControl("lblRef_ID") as Label;
var RefId = int.Parse(label.Text);
除非您有 Command="Select" 但您确实有 Command="Select",否则 rowcommand 事件不会触发 rowindexed 更改。但是,您没有 CommandArugment。
但是,既然您有 command=Select,那么请将您的代码移至 SelectedIndexChanged。问题是您不能 get/grab 来自“行”命令事件的行,因为该行没有更改。 rowcommand 事件在 SelectedIndexChanged 之前触发。如前所述,除非您使用特殊命令(删除或 select),否则 SelectedIndexChanged 不会触发。
所以行索引不会在行命令事件中设置。但是,这意味着对于您的 select 按钮(和删除),您的代码将(可以)在 rowindexchanged 中,“如果”您对命令使用 say select(或“删除”)。如果您使用自定义命令名称,则不会触发 rowindexedchanged。
您可以继续使用 rowcommand,但您需要 CommandArugment 来传递行的 PK 或行的索引。
所以你在这里有两个选择。 (实际上是 3 个)。
您可以设置该按钮的CommandArugment 值。 要么去PK,要么就用这个
CommandArgument ='<%# Container.DataItemIndex%>'
我注意到在你的命令设置中,你没有设置 CommandArugment - 你必须在你的标记中设置它。上面的表达式将 return 索引放入网格中。
您可以将代码移动到 rowindexchanged。
不过,还有第三种选择。您可以在行命令中获取行索引。所以从地狱的深处,你可以使用这个惊人的疯狂声明:
If e.CommandName = "MySelect" Then
' now get row.
Dim gvRow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Debug.Print("row index = " & gvRow.DataItemIndex)
Dim MyLabel As Label = gvRow.FindControl("HotelName")
Debug.Print("Value ref id = " & MyLabel.Text)
End If
注意非常小心: 对于模板化字段 - 我们使用查找控件。对于非模板字段,则可以按单元格引用。
现在超级获奖丑陋的方式来获取行中的行命令?
在 C# 中它看起来像这样:
{
GridViewRow gvRow = (GridViewRow)(Control)e.CommandSource.NamingContainer;
}
因此,您必须使用上面的方法,或者如前所述,将您的代码移至 SelectedIndexChanged。
既然你只有一个 select 和删除按钮,那么我建议 SelectedIndexChanged。但是,如果您要说引入另一个按钮 - 那么您将不想使用 Select 或 Delete,而是您选择的自定义命令名称(比如 CommandName="MyViewer")。在这种情况下,SelectedIndexChanged 将不会触发。但随后您将有办法确定单击了哪个按钮(超出 select 和删除命令的需要)。
So in summary:
You have about 3+ ways to do this.
Keep in mind that in row command, the selected index event has NOT yet fired.
Keep in mind that if you use a custom row command name, then selected index WILL NOT fire.
Keep in mind that you can pick up the selected row with that award winning ugly line of code.
Keep in mind that you can't use cells() collection for your custom templated columns - so use find control.
Keep in mind that selectedindex in row command event has NOT changed nor fired yet.
由于您正在使用 select,因此您可以选择行命令事件或 selectedindex 更改事件 - 它们都会在您的情况下触发。