Binding dataset to gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyabhaskar
    New Member
    • Dec 2008
    • 32

    Binding dataset to gridview

    hi,
    I want to bind the data from the Dataset to GridView
    When I run my code I get the error
    The type or namespace name 'DataRows' could not be found (are you missing a using directive or an assembly reference?)
    I have called the following assemblies
    using System;
    using System.Data;
    using System.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.Data.Sql Client;
    using System.Web.Conf iguration;

    please help.


    thanks
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Originally posted by satyabhaskar
    hi,
    I want to bind the data from the Dataset to GridView
    When I run my code I get the error
    The type or namespace name 'DataRows' could not be found (are you missing a using directive or an assembly reference?)
    I have called the following assemblies
    using System;
    using System.Data;
    using System.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.Data.Sql Client;
    using System.Web.Conf iguration;

    please help.


    thanks
    Hi satyabhaskar,

    Binding a DataSet to GridView is straight forward...Pleas e post some sample code..
    Code:
    SqlConnection con = new SqlConnection("");
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter("stored proc name/query", con);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;//Text
    
    da.Fill(ds);
    
    gridview1.DataSource = ds;
    gridview1.DataBind();
    Last edited by Frinavale; Dec 9 '11, 03:15 PM. Reason: Removed reference to a moderation action.

    Comment

    • preetis
      New Member
      • Dec 2011
      • 1

      #3
      bind the dataset with grid view

      Code:
      //connect to database via dataset & dataadapter
      SqlConnection con = new SqlConnection("server=localhost;Initial Catalog=databasename;uid=;pwd=;");
      DataSet ds = new DataSet();
       SqlDataAdapter da = new SqlDataAdapter("stored proc name", con);
       da.SelectCommand.CommandType = CommandType.StoredProcedure;
       da.Fill(ds);
      
       //create gridview by code & bind dataset to it
       GridView g = new GridView();
       Page.Controls.Add(g);
       g.DataSource = ds;
       Page.DataBind();
      regards

      Preeti
      Last edited by Frinavale; Dec 9 '11, 03:09 PM. Reason: Added code tags

      Comment

      • Shabir Ahmed
        New Member
        • Dec 2011
        • 1

        #4
        Code:
        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.";
              }
        Last edited by Frinavale; Dec 9 '11, 03:10 PM. Reason: Added code tags.

        Comment

        • 007007
          New Member
          • Dec 2011
          • 1

          #5
          Data Bind to GridView

          Code:
          SqlConnection con = new SqlConnection("server=localhost;Initial Catalog=databasename;uid=;pwd=;");
          DataSet ds = new DataSet();
          SqlDataAdapter da = new SqlDataAdapter("stored proc name", con);
          da.SelectCommand.CommandType = CommandType.StoredProcedure;
          da.Fill(ds);
          
          
          GridView grid = new GridView();
          Page.Controls.Add(grid);
          grid.DataSource = ds;
          Page.DataBind();
          Last edited by Frinavale; Dec 9 '11, 03:10 PM. Reason: Added code tags.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            007007,

            Do you have a question?
            The code that you have posted already exists within this thread as a part of the answer...Please clarify the reason for your posted code.

            -Frinny

            Comment

            Working...