使用模板列VB.net更新DataGrid

时间:2012-01-23 02:38:17

标签: asp.net .net vb.net

我尝试更新,但是我收到一个错误“索引超出限制。我不应该是负数,应该小于集合的大小”这个问题在调试中这个问题“Dim courseId As String = gridViewCourse.DataKeys (e.RowIndex).Values( “CourseId”)。的ToString()“

这是我的网格asp代码

 <asp:GridView ID="gridViewCourse" 
  runat="server" 
  AutoGenerateColumns="False" 
  onrowcancelingedit="gridViewCourse_RowCancelingEdit" 
  onrowdeleting="gridViewCourse_RowDeleting" onrowediting="gridViewCourse_RowEditing" 
  onrowupdating="gridViewCourse_RowUpdating" 
  onrowcommand="gridViewCourse_RowCommand"          
  ShowFooter="True">


   <Columns>
   <asp:TemplateField>
   <EditItemTemplate>
   <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
    <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
     </EditItemTemplate>
     <ItemTemplate>
         <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
         <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
      </ItemTemplate>
      <FooterTemplate>
      <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
    </FooterTemplate>
     </asp:TemplateField>

     <asp:TemplateField HeaderText="Id Cours">
     <EditItemTemplate>
     <asp:Label ID="lbleditId" runat="server" Text='<%#Eval("CourseId") %>'/>
     </EditItemTemplate>
      <ItemTemplate>
      <asp:Label ID="lblitemId" runat="server" Text='<%#Eval("CourseId") %>'/>
       </ItemTemplate>
      <FooterTemplate>
           <asp:TextBox ID="txtftrId" runat="server"/>
           <asp:RequiredFieldValidator ID="rfvId" runat="server" ControlToValidate="txtftrId" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate> 
               </asp:TemplateField>


               <asp:TemplateField HeaderText="Cours">
                    <EditItemTemplate>
                           <asp:TextBox ID="txtCours" runat="server" Text='<%#Eval("CourseName") %>'/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblCours" runat="server" Text='<%#Eval("CourseName") %>'/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrCours" runat="server"/>
                    <asp:RequiredFieldValidator ID="rfvCours" runat="server" ControlToValidate="txtftrCours" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate>
               </asp:TemplateField>


               <asp:TemplateField HeaderText="Prix">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtPrix" runat="server" Text='<%#Eval("Price") %>'/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblPrix" runat="server" Text='<%#Eval("Price") %>'/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrPrix" runat="server"/>
                    <asp:RequiredFieldValidator ID="rfvPrix" runat="server" ControlToValidate="txtftrPrix" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText="Tuteur">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtTuteur" runat="server" Text='<%#Eval("Tutor") %>'/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblTuteur" runat="server" Text='<%#Eval("Tutor") %>'/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtftrTuteur" runat="server"/>
                    <asp:RequiredFieldValidator ID="rfvTuteur" runat="server" ControlToValidate="txtftrTuteur" Text="*" ValidationGroup="validaiton"/>
                    </FooterTemplate>
              </asp:TemplateField>
        </Columns>
     </asp:GridView>

这是我的更新方法

     Protected Sub gridViewCourse_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

    Dim courseId As String = gridViewCourse.DataKeys(e.RowIndex).Values("CourseId").ToString()
    Dim courseName As TextBox = DirectCast(gridViewCourse.Rows(e.RowIndex).FindControl("CourseName"), TextBox)
    Dim price As TextBox = DirectCast(gridViewCourse.Rows(e.RowIndex).FindControl("Price"), TextBox)
    Dim tutor As TextBox = DirectCast(gridViewCourse.Rows(e.RowIndex).FindControl("Tutor"), TextBox)
    con.Open()
    Dim cmd As New SqlCommand(("update Courses set CourseName='" + courseName.Text & "',Price='" + price.Text & "',Tutor='") + tutor.Text & "' where CourseID=" & courseid, con)
    cmd.ExecuteNonQuery()
    con.Close()
    lblresult.ForeColor = Color.Green
    lblresult.Text = " Details Updated successfully"
    gridViewCourse.EditIndex = -1
    BindCoursesDetails()
End Sub

所以这一行导致我上面提到的错误

      Dim courseId As String = gridViewCourse.DataKeys(e.RowIndex).Values("CourseId").ToString()

这一行假设获取CourseId的值以便稍后用于更新

谢谢弗兰克

1 个答案:

答案 0 :(得分:1)

您需要设置GridView控件的DataKeyNames属性。

<asp:GridView ID="gridViewCourse" 
  runat="server" 
  AutoGenerateColumns="False" 
  onrowcancelingedit="gridViewCourse_RowCancelingEdit" 
  onrowdeleting="gridViewCourse_RowDeleting" onrowediting="gridViewCourse_RowEditing" 
  onrowupdating="gridViewCourse_RowUpdating" 
  onrowcommand="gridViewCourse_RowCommand" 
  datakeynames="CourseId"
  ShowFooter="True">

编辑:(来自评论)

您的更新声明无效。

Dim sql="UPDATE Courses set CourseName=@CourseName,Price=@Price,Tutor=@Tutor Where CourseID=@CourseID"

cmd=New SqlCommand(sql,con)
cmd.Parameters.Add("@CourseName",SqlDbType.VarChar,30).Value=courseName.Text
cmd.Parameters.Add("@Price",SqlDbType.VarChar,20).Value=price.Text
cmd.Parameters.Add("@Tutor",SqlDbType.VarChar,30).Value=tutor.Text
cmd.Parameters.Add("@CourseID",SqlDbType.VarChar,10).Value=courseid

con.Open()
cmd.ExecuteNonQuery()
cn.Close();