【问题标题】:How To select Particular Cell Value in Grid View如何在网格视图中选择特定的单元格值
【发布时间】:2021-02-27 09:27:44
【问题描述】:

我想选择特定单元格值并将用于绑定控件但我无法选择单元格值 它没有检索数据

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;
    }
}

在调试时我可以看到类似错误:

“输入字符串的格式不正确”我做错了什么

我的表结构

________________________________________
Ref_ID|  CompanyName  |  DivisionName   |
______|_______________|_________________|
6     |   Company     |    Sales        |
8     |   Company     |    Sales        |
14    |   Company     |    Sales        |
______|_______________|_________________|

RefID 我已将其视为“int”和 CompanyName 和 DivisionName “string”,所以我输入的字符串格式错误,

我哪里做错了请给我同样的建议

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    rowcommand 事件不会触发 rowindexed 更改,除非您有 Command="Select" 但您确实有 Command="Select"。但是,您没有 CommandArugment。

    但是,既然您确实有 command=Select,那么请将您的代码移动到 SelectedIndexChanged。问题是您无法从“row”命令事件中获取/抓取该行,因为该行没有更改。 rowcommand 事件在 SelectedIndexChanged 之前触发。如前所述,除非您使用特殊命令(删除或选择),否则 SelectedIndexChanged 不会触发。

    因此不会在行命令事件中设置行索引。但是,这将意味着对于您的选择按钮(和删除),您的代码将(可以)在 rowindexchanged “如果”您对命令使用说选择(或“删除”)。如果您使用自定义命令名称,则不会触发 rowindexedchanged。

    您可以继续使用 rowcommand,但您需要 CommandArugment 传递行的 PK 或行的索引。

    所以你在这里有两个选择。 (实际上是 3 个)。

    您可以设置该按钮的 CommandArugment 的值。 要么去PK,要么你可以用这个

    CommandArgument ='<%# Container.DataItemIndex%>' 
    

    我注意到在你的命令设置中,你没有设置 CommandArugment - 你必须在你的标记中设置它。上面的表达式会将索引返回到网格中。

    您可以将代码移动到 rowindexchanged。

    但是,还有第三个选择。您可以在 rowcommand 中获取 rowindex。所以从地狱的深处,你可以使用这个惊人的疯狂的声明:

    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。

    由于您只有一个选择和删除按钮,所以我建议使用 SelectedIndexChanged。但是,如果您要说引入另一个按钮 - 那么您将不想使用 Select 或 Delete,而是使用您选择的自定义命令名称(例如 CommandName="MyViewer")。在这种情况下,SelectedIndexChanged 将不会触发。但是,您将有一种方法可以确定单击了哪个按钮(无需选择和删除命令)。

    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 更改事件 - 它们都会在您的情况下触发。

    【讨论】:

    • 感谢您提供如此好的解释,非常感谢您的关注和时间,非常感谢
    【解决方案2】:

    问题是单元格不包含任何文本,而是标签lblRef_ID。所以你需要访问标签,而不是单元格值。

    var label = e.Row.FindControl("lblRef_ID") as Label;
    var RefId = int.Parse(label.Text);
    

    【讨论】:

    • 行和标签出现错误
    • 您可以在所选索引中使用/获取 e.row 已更改,但不能在 row 命令中使用。如前所述,移动你的代码,或者使用那个超级丑陋的语句——一旦你使用了 Mr. Ugly 语句来获取行,你就可以使用 find 控制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2023-04-08
    相关资源
    最近更新 更多