C# gridview e.CommandArgument字符串格式不正确

C# gridview e.CommandArgument字符串格式不正确,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试将一个pdf从我的数据库带到我的gridview,并允许用户单击它并下载pdf。我试着按照这里解决的问题: 但是,我得到以下错误: Input string was not in a correct format. 以下是我的asp.net代码: <Columns> <asp:CommandField ShowEditButton="True" ControlStyle-CssClass="savefile"/>

我正在尝试将一个pdf从我的数据库带到我的gridview,并允许用户单击它并下载pdf。我试着按照这里解决的问题:

但是,我得到以下错误:

Input string was not in a correct format.
以下是我的asp.net代码:

     <Columns>
         <asp:CommandField ShowEditButton="True" ControlStyle-CssClass="savefile"/>
         <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
             ReadOnly="True" SortExpression="ID" />
         <asp:BoundField DataField="event_name" HeaderText="event_name" 
             SortExpression="event_name" />

         <asp:TemplateField HeaderText="PDF">

   <ItemTemplate>
       <asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" HeaderText="Download"  runat="server" Text="Button" />


    </ItemTemplate>

   <EditItemTemplate>

       <asp:FileUpload ID="FileUpload1" runat="server" /> // shown only in edit mode

   </EditItemTemplate>

  </asp:TemplateField>

     </Columns>
 </asp:GridView>

错误行在我的c代码中是这样的:

提前谢谢你们的帮助,伙计们

编辑--------------------------------------------------

我的按钮字段现在如下所示:

<asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" CommandArgument='<%#Container.DataItemIndex%>' HeaderText="Download"  runat="server" Text="Button" />

我仍然收到同样的错误

您没有指定
命令参数
。因此,发生错误是因为您试图将空白字符串转换为整数

将行索引添加为
命令参数

尝试将此添加到
按钮
控件:

<asp:Button ID="Button1"
            ButtonType="Link"
            CommandName="DownloadFile"
            CommandArgument='<%#Container.DataItemIndex%>'
            HeaderText="Download"
            runat="server"
            Text="Button" />

您没有为
CommandArgument
赋值,而是在
RowCommand
中访问它们,这是导致错误的原因

 <asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" HeaderText="Download"  runat="server" Text="Button" CommandArgument="1" />

我将它添加到我的按钮控件中,但仍然得到相同的错误。我还尝试更改为e.Row.RowIndex,但出现了另一个错误GridViewCommandEventArgs'不包含'Row'的定义@user967713请参见我的编辑。使用
CommandArgument='
仍会收到相同的错误。我编辑了OP并显示了我的按钮字段和c代码行现在的样子。@user967713将
e.Row.RowIndex
更改回
e.CommandArgument
。对不起,我弄错了,我把数据绑定事件和RowCommand事件混淆了。只有DataBound可以访问
e.Row
。即使在将CommandArguements更改为CommandArgument=''之后,我仍然会遇到相同的错误
int index = Convert.ToInt32(e.CommandArgument);
<asp:Button ID="Button1"
            ButtonType="Link"
            CommandName="DownloadFile"
            CommandArgument='<%#Container.DataItemIndex%>'
            HeaderText="Download"
            runat="server"
            Text="Button" />
 <asp:Button ID="Button1" ButtonType="Link" CommandName="DownloadFile" HeaderText="Download"  runat="server" Text="Button" CommandArgument="1" />
CommandArgument='<%#eval("DataField")'
int index = Convert.ToInt32(e.CommandArgument.ToString());