1

Following code sets the AutoGenerateColumns to true in form_load event:

gvTables.AutoGenerateColumns = true;

Following is the code to bind gridview gvTables:

Query ="select name from sys.tables";  
DataSet tablesDataSet = new DataSet("TableData");
OleDbDataAdapter tablesDataAdaptor = new OleDbDataAdapter(Query, this.Connection);
tablesDataAdaptor.Fill(tablesDataSet);
gvTables.DataSource = tablesDataSet;

The Code runs fine. Even data is been retrieved into dataset tableDataSet, but the value is not been displayed into gridview control.

Priscilla Jobin
  • 609
  • 7
  • 19

2 Answers2

0

I Got the solution for this question. The solution is been posted in an orderly manner below:

1) Declaration of a new variable in the class is as given below:

private DataSet _Records = null;    

2) Set the property of the new variable:

public DataSet Records
{ get { return this._Records; } set { this._Records = value; } }

2) Following is the code in the click event:

Query ="select name from sys.tables";  

GetDataSetForQuery(Query);//Set the data set using GetDataSetForQuery(Query) method
BindingSource bs = new BindingSource();
bs.Datasource = this.Records.Tables[0] ;
gvTables.Datasource = bs;

4) Following is the code for GetDataSetForQuery(string sqlQuery) method:

    private bool GetDataSetForQuery(string sqlQuery)
    {
        try
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery, this.Connection);
            da.Fill(ds);
            this.Records = ds;
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
        finally { }
    }
Priscilla Jobin
  • 609
  • 7
  • 19
-1

Call the following function after your code.

gvTables.SetDataBinding(tablesDataSet , "Data");
EZLearner
  • 1,614
  • 16
  • 25