Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在网格视图中选择特定单元格值_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何在网格视图中选择特定单元格值

C# 如何在网格视图中选择特定单元格值,c#,asp.net,gridview,C#,Asp.net,Gridview,我想选择特定的单元格值,并将用于绑定控件,但我无法选择单元格值 它不能检索数据 ASPX代码 <asp:GridView ID="grdLogedUserDetails" OnRowDeleting="grdLogedUserDetails_RowDeleting" OnRowDataBound="grdLogedUserDetails_RowDataBound" runat="serve

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

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>
调试时,我可以看到如下错误:

输入字符串的格式不正确,我做错了

我的桌子结构

________________________________________
Ref_ID|  CompanyName  |  DivisionName   |
______|_______________|_________________|
6     |   Company     |    Sales        |
8     |   Company     |    Sales        |
14    |   Company     |    Sales        |
______|_______________|_________________|
RefID我采用了int、CompanyName和DivisionName字符串,因此我的输入字符串格式错误


如果我做错了,请建议我使用相同的

问题是单元格不包含任何文本,而是包含标签lblRef\u ID。因此,您需要访问标签,而不是单元格值

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

rowcommand事件不会触发rowindexed更改,除非您使用Command=Select,但确实使用Command=Select。但是,您没有命令段

但是,由于您确实有command=Select,因此请将代码移动到SelectedIndexChanged。问题是您无法从row命令事件获取/获取该行,因为该行尚未更改。rowcommand事件在SelectedIndexChanged之前激发。如前所述,除非使用特殊命令delete或select,否则SelectedIndexChanged不会触发

因此,rowindex将不会在row命令事件中设置。但是,这将意味着,如果对命令使用select或delete,则对于select按钮和delete,您的代码将可以在ROWINDEXCHANGE中。如果使用自定义命令名,则不会激发rowindexedchanged

您可以继续使用rowcommand,但需要使用commandargment传递行的主键或行的索引

所以你有两个选择。实际上是3

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

CommandArgument ='<%# Container.DataItemIndex%>' 
请非常小心地注意: 对于模板化字段,我们使用find控件。对于非临时字段,则可以按单元格引用

现在,超级获奖的丑陋方式,以获得排在排命令

在C中,它将如下所示:

{
  GridViewRow gvRow = (GridViewRow)(Control)e.CommandSource.NamingContainer;
}
因此,您必须使用上面的命令,或者如上所述,将代码移动到SelectedIndexChanged

由于您只有一个选择和删除按钮,因此我建议更改SelectedIndex。但是,如果您要说引入另一个按钮,那么您将不希望使用Select或Delete,而是希望使用您选择的自定义命令名say CommandName=MyViewer。在这种情况下,则SelectedIndexChanged将不会激发。但是,除了select和delete命令之外,您还可以确定单击了哪个按钮

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,所以您可以选择row命令事件或selectedindex changed事件-在您的情况下,它们都将触发。

我发现row和LABEL上有错误。您可以使用/get e.row更改所选索引,但不能使用row命令。如前所述,移动您的代码,或者使用那个超级丑陋的语句-一旦您使用Mr.Shugh语句获取行,您就可以使用find control。感谢这么好的解释,非常感谢您的关注和时间,非常感谢
{
  GridViewRow gvRow = (GridViewRow)(Control)e.CommandSource.NamingContainer;
}
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.