C PWL
C PWL

Reputation: 519

How to Display DataSet in ListView/GridView

Let's say I retrieved a DataSet from database. After that I want to display at ListView / GridView of ASP.Net C#. How can I do this? Any sample for me?

Upvotes: 1

Views: 16529

Answers (3)

Dewasish Mitruka
Dewasish Mitruka

Reputation: 2896

Try this

if(datasetObject.tables.count > 0)
    {
    GridView.DataSource = datasetObject;
    GridView.DataBind();

    }
else
{
  lable.Text = "No Record Found";
}

Upvotes: 4

Shyju
Shyju

Reputation: 218702

Set the dataset as the DataSource property value of the grid and then call the DataBind() method.

from msdn

http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx

void Page_Load(Object sender, EventArgs e)
{
// This example uses Microsoft SQL Server and connects
// to the Northwind sample database. The data source needs
// to be bound to the GridView control only when the 
// page is first loaded. Thereafter, the values are
// stored in view state.                      
if(!IsPostBack)
{

  // Declare the query string.
  String queryString = 
    "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";

  // Run the query and bind the resulting DataSet
  // to the GridView control.
  DataSet ds = GetData(queryString);
  if (ds.Tables.Count > 0)
  {
    AuthorsGridView.DataSource = ds;
    AuthorsGridView.DataBind();
  }
  else
  {
    Message.Text = "Unable to connect to the database.";
  }

}   
}

Assuming AuthorsGridView is your GridView control's ID and GetData method returns a Dataset with data.

Upvotes: 1

Rahul
Rahul

Reputation: 77846

Use the DataBind() method of GridView to do that. like

GridView.DataSource = ds;
GridView.DataBind();

Upvotes: 1

Related Questions