locked
how to fill datagridview with table column names RRS feed

  • Question

  • User920022889 posted

    i want to fill datagridview with table column names. i created student table in database. i want to fill student table column names  in data grid view. please help me.

    here i have one condition. datagirdview contain only one column(rows are no of table column names) in that column i want to fill tables column names. howw to done

    Thursday, November 14, 2013 11:57 AM

Answers

  • User920022889 posted

    thanks..

    i done this.

    first i want retrived data from database,stored in datatable.

    after that i created datatable dynamically then after i filled second data table rows with first data table column names, after  i bind second datatable to datagridview.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 17, 2013 10:30 AM

All replies

  • User-1965857832 posted

    See if this helps

    protected void Page_Load(Object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
    
            // Declare the query string.
            String queryString ="Select [Column1], [Column2], etc From [Student]";
    
            // Run the query and bind the resulting DataSet
            // to the GridView control.
            DataSet ds = GetData(queryString);
            if (ds.Tables.Count > 0)
            {
                UrGridView.DataSource = ds;
                UrGridView.DataBind();
            }
        }
    
    }
    
    private DataSet GetData(String queryString)
    {
        // Retrieve the connection string if its stored in the Web.config file
        String connectionString = ConfigurationManager.ConnectionStrings["UrConnectionString"].ConnectionString;
        //else set it manually as below
        //String connectionString = @"Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;Password=myPassword;";
    
        DataSet ds = new DataSet();
    
        try
        {
            // Connect to the database and run the query.
            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
    
            // Fill the DataSet.
            adapter.Fill(ds);
    
        }
        catch (Exception ex)
        {
            // The connection failed. Display an error message.
        }
    
        return ds;
    }
    Thursday, November 14, 2013 12:04 PM
  • User920022889 posted

    i have to done in windows form. iam using data grid view

    Thursday, November 14, 2013 12:08 PM
  • User920022889 posted

    thanks..

    i done this.

    first i want retrived data from database,stored in datatable.

    after that i created datatable dynamically then after i filled second data table rows with first data table column names, after  i bind second datatable to datagridview.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 17, 2013 10:30 AM