Datagrid rowcommand在搜索结果后显示不正确的值 [英] Datagrid rowcommand shows incorrect value after my search result

查看:61
本文介绍了Datagrid rowcommand在搜索结果后显示不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的项目看起来很简单。我的表单在加载时填充数据网格(来自数据库)

我有一个可以使用的搜索按钮,我现在可以过滤结果。

我还有一个onRowCommand,现在只显示所选行的详细信息。

但是,在页面加载时,它看起来很好,我可以毫无问题地检查详细信息。但是,当我进行搜索时。它给了我一个不同的价值。

例如。



日向[查看]

樱花[查看]

Tenten [查看]



如果搜索n

结果是





Hinata [查看]

Tenten [查看]



但是当我点击Tenten的视图 - 它显示了SAKURA :(



我的表格

Hello everyone,
My project seems simple. My form populates a datagrid on load (from a database)
I have a search button which works and I can now filter the results.
I also have an onRowCommand which, for now just shows the details of the the selected row.
However, at page load, it seems fine, i can check the details without problem. But, when I do a search. it shows me a different value.
for example.

Hinata [View]
Sakura [View]
Tenten [View]

if search "n"
the result is


Hinata [View]
Tenten [View]

but when i click View of Tenten - It shows SAKURA :(

My Form

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

        End Sub







Hello Again,
I kinda cleaned my code a bit. I also used a viewstate.. but I still get the error.
<pre lang="vb">
Protected Sub lnkSearch_Click(sender As Object, e As EventArgs) Handles lnkSearch.Click
        Dim strSearch As String
        
        strSearch = "SELECT Fid,RealName,Department FROM tblUsers" &
                                    " WHERE RealName LIKE '%" & txtName.Text & "%'"

        Dim constring As String = "Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=dbTOD_Sql;Integrated Security=True"
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand(strSearch, con)
                cmd.CommandType = CommandType.Text
                Using sda As New SqlDataAdapter(cmd)
                    Using dt As New DataTable()
                        sda.Fill(dt)

                        'Set AutoGenerateColumns False
                        GridView1.AutoGenerateColumns = False

                        'First time knowing ViewState. HOpe this works
                        ViewState("vsTable") = dt
                        'Reading the viewstate values.
                        Dim dt2 As DataTable = DirectCast(ViewState("vsTable"), DataTable)
                        GridView1.DataSource = dt2
                        GridView1.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End Sub
</pre>







受保护的子GridView1_RowCommand(发送者为对象,e为GridViewCommandEventArgs)



Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)

Dim row As GridViewRow = GridView1.Rows(rowIndex)



'访问单元格值。

Dim customerId As Integer = Integer.Parse(row.Cells(0).Text)

Dim name As String = row.Cells(1).Text

MsgBox(名称)'仅用于测试

End Sub



我尝试了什么:



编辑:我按照建议尝试了Viewstate,但我仍然收到错误。



我尝试过selectedIndexChange但是,我仍然得到相同的结果。我做了计算机重启,希望它只是一个错误,刷新会让问题消失。

我迷路了。我不知道该怎么办。




Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)

Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(rowIndex)

'Access Cell values.
Dim customerId As Integer = Integer.Parse(row.Cells(0).Text)
Dim name As String = row.Cells(1).Text
MsgBox (name) 'just for testing
End Sub

What I have tried:

Edited: I tried Viewstate as suggested but I still get the error.

I tried selectedIndexChange but, I still get the same results. I did Computer restarts hoping it was just a bug and a refresh would make the problem go away.
I am lost. I don't know what to do anymore.

推荐答案

检查你的user1值。如果你对你提到的所有事情使用相同的页面,那么使用viewstate []
Check your user1 value. if your using same page for all things which you have mentioned then use viewstate[]


这篇关于Datagrid rowcommand在搜索结果后显示不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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