简体   繁体   English

使用模板列VB.net更新DataGrid

[英]DataGrid update with template column VB.net

I try an update but I get an error"The Index was out limits.I shouldn't be negative and should be smaller than size of the collection" This concern this line in debug "Dim courseId As String = gridViewCourse.DataKeys(e.RowIndex).Values("CourseId").ToString()" 我尝试更新,但出现错误“索引超出限制。我不应为负,并且应小于集合的大小”。这与调试中的这一行有关:“ Dim courseId为String = gridViewCourse.DataKeys(e。 RowIndex).Values(“ CourseId”)。ToString()“

This my grid asp code 这是我的网格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>

This is my method to update 这是我的更新方法

     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

So this line cause the error I mentioned above 所以这行会导致我上面提到的错误

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

This line is suppose to get the value of CourseId to use it later for the update 该行假定获取CourseId的值,以供以后用于更新

Thanks Frank 谢谢弗兰克

You need to set DataKeyNames attribute of GridView control. 您需要设置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">

EDIT: (from the comment) 编辑:(从评论)

Your update statement is invalid. 您的更新声明无效。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM