Sunday, September 10, 2017

Get count of rows and columns from datatable in asp.net

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

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

Syntax
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;

VB.net 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

Example:
HTML Markup of webform

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Get count of rows and columns from datatable </title>   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        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">
            <Columns>
                <asp:BoundField HeaderText="Name" DataField="Name">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>              
                <asp:BoundField DataField="Phone" HeaderText="Phone">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Salary" HeaderText="Salary">
                  <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                  <asp:BoundField DataField="Department" HeaderText="Department">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
              <asp:BoundField DataField="EmailId" HeaderText="Email">
                  <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
             <asp:ImageField DataImageUrlField="ImagePath" ControlStyle-Height="100" ControlStyle-Width="100">
<ControlStyle Height="100px" Width="100px"></ControlStyle>
                </asp:ImageField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Add namespace

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

VB.net 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)
        {
            Fillgrid();
        }
    }
    public void Fillgrid()
    {
      
            SqlDataAdapter adp = new SqlDataAdapter("Select * from Employees", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            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;
            GridView1.DataBind();      
    }


VB.net 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
            Fillgrid()
        End If
    End Sub
    Public Sub Fillgrid()
        Try
            Dim adp As New SqlDataAdapter("Select * from Employees", con)
            Dim dt As New DataTable()
            adp.Fill(dt)
            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
                GridView1.DataBind()
            End If
         
        Catch ex As Exception
        End Try
    End Sub







No comments:

Post a Comment