ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Populate Windows Form DataGridView from specific range of rows using C# and VB.Net<p>Hi <a class="username" rel="satabeach"> satabeach</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Database</strong></span></p> <p>For this sample I have used of NorthWind database that you can download using the link given below.</p> <p><a href="" target="_blank">Download Northwind Database</a></p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">private void Form1_Load(object sender, EventArgs e) { string constr = @&#34;Server=.\SQL2005;DataBase=Northwind;UID=user;PWD=pass&#34;; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT City, Country FROM Customers&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); var rows = dt.Select().Skip(5).Take(10); DataRow[] dr = rows.ToArray(); DataTable dt1 = dr.CopyToDataTable(); dataGridView1.DataSource = dt1; } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim constr As String = &#34;Server=.\SQL2005;DataBase=Northwind;UID=user;PWD=pass&#34; Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT City, Country FROM Customers&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) Dim rows = dt.Select().Skip(5).Take(10) Dim dr As DataRow() = rows.ToArray() Dim dt1 As DataTable = dr.CopyToDataTable() dataGridView1.DataSource = dt1 End Using End Using End Using End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="" alt="" width="305" height="275" /></strong></span></p>, 12 Nov 2018 02:22:44 GMT