GridView中的数据匹配 [英] Data Matching in GridView
问题描述
我在gridview中有一个数据,例如....
i have a data in gridview like....
Name STD
A 9th
B 9th
C 8th
D 11th
E 12th
F 11th
G 9th
H 9th
在第二个gridview中,我想要类似...的数据.
in second gridview i want data like....
Player1 Player2
A C
B D
E F
G H
问题是,不要在同一STD之间进行匹配.如果是强制性的,那就没问题.就像"G vs H"之间的最后一场比赛.
在这里,当我单击按钮时,想要第二个gridview之类的数据.
谢谢....
我尝试使用以下代码,但无法正常工作.
problem is that dont want match between same STD. if it is compulsory then no problem. like last match between "G vs H".
here when i click on button then want to data like second gridview.
thanks....
i tried below code but its not working properly.
Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
Dim x, y As Integer
Dim first, sec As String
Dim newr As Integer
For x = 0 To DataGridView1.Rows.Count - 2
first = DataGridView1.Rows(x).Cells(0).Value
For y = 0 To DataGridView1.Rows.Count - 2
sec = DataGridView1.Rows(y).Cells(0).Value
If first <> sec Then
newr = DataGridView2.Rows.Add(+1)
DataGridView2.Rows(newr).Cells(1).Value = first
DataGridView2.Rows(newr).Cells(2).Value = sec
End If
Next
Next
End Sub
推荐答案
我在页面后面的代码是这个
My Code behind page is this
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LoadValues()
End Sub
Dim datatable As New DataTable
Public Function LoadValues()
datatable.Columns.Add("Name")
datatable.Columns.Add("Std")
datatable.Rows.Add("A", 9)
datatable.Rows.Add("B", 9)
datatable.Rows.Add("C", 8)
datatable.Rows.Add("D", 11)
datatable.Rows.Add("E", 12)
datatable.Rows.Add("F", 11)
datatable.Rows.Add("G", 9)
datatable.Rows.Add("H", 9)
GridView1.DataSource = datatable
GridView1.DataBind()
End Function
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim list As New List(Of String)
Dim count As Integer = datatable.Rows.Count
For i As Integer = 0 To count - 1 Step +1
For j As Integer = count - 1 To i Step -1
If datatable.Rows(i).Item("Std") <> datatable.Rows(j).Item("Std") Then
If Not list.Contains(datatable.Rows(i).Item("Name")) AndAlso Not list.Contains(datatable.Rows(j).Item("Name")) Then
list.Add(datatable.Rows(j).Item("Name"))
list.Add(datatable.Rows(i).Item("Name"))
Exit For
End If
End If
Next
Next
Dim datatable1 As New DataTable
datatable1.Columns.Add("Team1")
datatable1.Columns.Add("Team2")
Dim spe As Integer = (list.Count / 2)
For i = 0 To list.Count - 1 Step +2
datatable1.Rows.Add(list(i), list(i + 1))
Next
Dim firstquery = From b In datatable Select b.Item("Std")
Dim FinalQuery = From p In datatable Where Not firstquery.Contains(p.Item("Std")) Select New With {p} Distinct
GridView2.DataSource = datatable1
GridView2.DataBind()
End Sub
End Class
在default.aspx中创建一个新应用程序,只需添加两个gridview和一个按钮.
用上面的代码替换页面后面代码中的现有代码.然后检查并告知我.我没有遵循任何命名约定,只是放下了控件并让我知道.
希望对您有所帮助.
create a new application in the default.aspx just add two gridview and one button.
replace the existing code in the code behind page with the above code.and check and let me know. I have not followed any naming convention just drop the controls and let me know.
Hope this helps.
这可以帮助您入门:
http://www.daniweb. com/software-development/vbnet/threads/44575/round-robin-scheduling-using-threads-having-some-trouble [
may this will get you started:
http://www.daniweb.com/software-development/vbnet/threads/44575/round-robin-scheduling-using-threads-having-some-trouble[^]
巴特,
我用了这个功能
Hi Bhatt,
I used this function
Dim list As New List(Of String)
Dim count As Integer = datatable.Rows.Count
For i As Integer = 0 To count - 1 Step +1
For j As Integer = count - 1 To i Step -1
If datatable.Rows(i).Item("Std") <> datatable.Rows(j).Item("Std") Then
If Not list.Contains(datatable.Rows(i).Item("Name")) AndAlso Not list.Contains(datatable.Rows(j).Item("Name")) Then
list.Add(datatable.Rows(j).Item("Name"))
list.Add(datatable.Rows(i).Item("Name"))
Exit For
End If
End If
Next
Next
Dim datatable1 As New DataTable
datatable1.Columns.Add("Team1")
datatable1.Columns.Add("Team2")
Dim spe As Integer = (list.Count / 2)
For i = 0 To list.Count - 1 Step +2
datatable1.Rows.Add(list(i), list(i + 1))
Next
其中datatable是具有名称和标准的表的表,而datatable1将根据您的要求生成.
希望这对您有帮助...:)
Where datatable is the table which is having your table with name and standard and the datatable1 will result with your requirement.
Hope this helps...:)
这篇关于GridView中的数据匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!