我在VB.NET中从gridview中删除一行时遇到问题 [英] I have a problem in deleting a row from gridview in VB.NET

查看:52
本文介绍了我在VB.NET中从gridview中删除一行时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码我用来删除gridview中的行

The Below codes i used to delete the row in the gridview

Public Sub BindData()
        com.CommandText = "Select * from markentrysem3"
        com.Connection = con
        ad = New SqlDataAdapter(com)
        ad.Fill(d)
        con.Open()
        com.ExecuteNonQuery()
        GridView1.DataSource = d
        GridView1.DataBind()

        con.Close()
    End Sub
    Protected Sub OnRowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim com As New SqlCommand("DELETE FROM markentrysem3 WHERE StudId= + TextBox2.Text & ")
        com.Parameters.AddWithValue("StudId", StudId)
        com.Connection = con
        con.Open()
        com.ExecuteNonQuery()
        con.Close()
        Me.BindData()
        
    End Sub
    Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowIndex <> GridView1.EditIndex Then
            TryCast(e.Row.Cells(0).Controls(0), LinkButton).Attributes("onclick") = "return confirm('Do you want to delete this row?');"
        End If
    End Sub

    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.BindData()
        End If
    End Sub



我得到了错误如:DataSource和DataSourceID都在'GridView1'上定义。删除一个定义。



我尝试过:



以上编码用于从gridview中删除行,并且我在编辑更新和取消时遇到问题。


And i getting the error like:Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.

What I have tried:

The above coding is used to delete the row from gridview and also i have a problem in edit update and cancel.

推荐答案

错误清楚地告诉你出了什么问题以及该怎么做修复问题。



您在设计时定义了数据绑定(即通过设计模式下IDE中的属性窗口)



您还在BindData函数中执行数据绑定。



只选择一种方法,不能同时选择两种方法。



我建议您在选择DataGrid时进入属性窗口并删除DataSourceID(如果设置则删除DataMember和DataKeyNames)。



由于您没有解释编辑更新和取消的问题,我们无法帮助您。
The error clearly tells you what is wrong and what to do to fix the problem.

You have databinding defined at Design time (i.e. via the Properties window in the IDE in Design mode)

You are also performing databinding in the function BindData.

Choose one method only, you cannot do both.

I suggest you go to the Properties window when the DataGrid is selected and remove the DataSourceID (and DataMember and DataKeyNames if set).

As you haven't explained what the problem is with edit update and cancel we can't help you there.


这篇关于我在VB.NET中从gridview中删除一行时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆