Get count of rows and columns from datatable in - ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

Get count of rows and columns from datatable in

In this article I am going how to get count of rows and columns from datatable in

I want to know how many rows and columns in datatable or dataset?

C# code

int row = dt.Rows.Count;
int column = dt.Columns.Count;
//dataset row count
  int row = ds.Tables[0].Rows.Count;
  int column = ds.Tables[0].Columns.Count; code

Dim row As Integer = dt.Rows.Count
Dim column As Integer = dt.Columns.Count
dataset row count
Dim row As Integer = ds.Tables(0).Rows.Count
Dim column As Integer = ds.Tables(0).Columns.Count

HTML Markup of webform

<html xmlns="">
<head runat="server">
  <title>Get count of rows and columns from datatable </title>   
    <form id="form1" runat="server">
        No. of rows: <asp:Label ID="lblrow" runat="server"></asp:Label><br />
        No. of columns : <asp:Label ID="lblcolumn" runat="server"></asp:Label>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <asp:BoundField HeaderText="Name" DataField="Name">
                <HeaderStyle HorizontalAlign="Left" />
                <asp:BoundField DataField="Phone" HeaderText="Phone">
                <HeaderStyle HorizontalAlign="Left" />
                <asp:BoundField DataField="Salary" HeaderText="Salary">
                  <HeaderStyle HorizontalAlign="Left" />
                  <asp:BoundField DataField="Department" HeaderText="Department">
                <HeaderStyle HorizontalAlign="Left" />
              <asp:BoundField DataField="EmailId" HeaderText="Email">
                  <HeaderStyle HorizontalAlign="Left" />
             <asp:ImageField DataImageUrlField="ImagePath" ControlStyle-Height="100" ControlStyle-Width="100">
<ControlStyle Height="100px" Width="100px"></ControlStyle>

Add namespace

C# Code
using System.Configuration;
using System.Data.SqlClient;
using System.Data; code
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Bind Gridview
C# Code

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    public void Fillgrid()
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Employees", con);
            DataTable dt = new DataTable();
            if (dt.Rows.Count > 0)
            int row = dt.Rows.Count;
            int column = dt.Columns.Count;
            lblcolumn.Text = column.ToString();
            lblrow.Text = row.ToString();
            GridView1.DataSource = dt;
    } code

Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Public Sub Fillgrid()
            Dim adp As New SqlDataAdapter("Select * from Employees", con)
            Dim dt As New DataTable()
            If dt.Rows.Count > 0 Then
                Dim row As Integer = dt.Rows.Count
                Dim column As Integer = dt.Columns.Count
                lblrow.Text = row.ToString()
                lblcolumn.Text = column.ToString()
                GridView1.DataSource = dt
            End If
        Catch ex As Exception
        End Try
    End Sub

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.