Home > Software engineering >  Refer to the VB.NET database a line under a line on the display
Refer to the VB.NET database a line under a line on the display

Time:09-20

Vb.net after connecting to the database, run the program how to let DataGridView1 does not display the data of database table, when click button shows the next line, DataGridView1 shows the first line of the table, then click on the next line shows the next row data, DataGridView1 show only a row, on a line by the same token, the big brand,

CodePudding user response:

Filled with sqldataadapter detected data into datatable1,
Then, define a variable rowno as the line number, on a line/line of the two button click event in
Define a temporary datatable2
Dim datatable2 as datatable=datatable1. Clone 'copy table structure
Datatable2. Rows. The add (datatable1 rows (rowno). Itemarray) 'copy rowno corresponding row
Datagridview1. The datasource=datatable2//the temporary datatable bound to the datagridview

This demand is a little special because of you, in fact, don't do it usually, but by setting the dataview filter property to the content of the screening of the datatable, sets the datagridview data sources to the dataview again, if you can press the row number to screening, can also try the dataview,

CodePudding user response:

Dataview filter attribute to filter should be how to fill out, all columns in the table,,

CodePudding user response:

Because of LINQ are not ripe, so there is no introduction, I wrote a few examples, can be used as a reference
 Dim dtSource As New DataTable 'dtSource is need to bind to the datagridview DataTable, As an example, 
Built With dtSource. The Columns' table structure, data
. The Add (" col1 ")
. The Add (" col2 ")
. The Add (" col3)
End With
DtSource. Rows. The Add ({" r1c1 ", "r1c2", "r1c3"})
DtSource. Rows. The Add ({" r2c1 ", "r2c2", "r2c3"})
DtSource. Rows. The Add ({" r3c1 ", "r3c2", "r3c3"})
DtSource. Rows. The Add ({" r4c1 ", "r4c2", "r4c3"})

'DefaultView datatable is the default view, when you need to show the same table at the same time different
'content can also define your own several DataView,
'the screen demonstration is to apply a col1="r3c1" filter to the DefaultView,
'if there a way to directly by the screening line number here, you can write in this
'dtSource. DefaultView. RowFilter="col1=' r3c1 '"

'binding view to the DataGridView
'DataGridView1. The DataSource=dtSource. DefaultView


'if you don't find a way to press the row number screening, there can be two ways
'way 1, query, add upward number (make sure good collation)
'the SQL statement is similar to

'select *,
'ROW_NUMBER () over (order by sort field asc) as RowNo
'the from devices

'other operations are same as above,
'the DefaultView. RowFilter="RowNo=" & amp; Record the line number of variable
'and then bind

'approach 2, query, normally after dtSource, add a column, iterate through each line, the line number is assigned to the column of
DtSource. Columns. The Add (" RowNo ")
Dim rowno As Integer=0
For Each Dr As DataRow dtSource. In Rows
Rowno +=1
Dr (" RowNo ")=RowNo
Next
DtSource. DefaultView. RowFilter="RowNo=3"
DataGridView1. The DataSource=dtSource. DefaultView

CodePudding user response:

Above is too bothersome, but it is no problem, just tried it on LINQ, a word can be pulled out the data from the datatable,
 
Dim dtSource As New DataTable
With dtSource. Columns
. The Add (" col1 ")
. The Add (" col2 ")
. The Add (" col3)
End With
DtSource. Rows. The Add ({" r1c1 ", "r1c2", "r1c3"})
DtSource. Rows. The Add ({" r2c1 ", "r2c2", "r2c3"})
DtSource. Rows. The Add ({" r3c1 ", "r3c2", "r3c3"})
DtSource. Rows. The Add ({" r4c1 ", "r4c2", "r4c3"})

'the following two sentences equivalent, the parameters of the Skip method, article indicates how much need to Skip, then in the rest of the sequence, Take article 1,
'DataGridView1. The DataSource=dtSource. AsEnumerable. Skip (1) Take (1) CopyToDataTable ()
DataGridView1. The DataSource=
(the From r dtSource In the Select r.) Skip (1) Take (1) CopyToDataTable ()
  • Related