Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Can DataGridView be displayed without a dataset from a database? How is this done?
Posted
Updated 12-May-11 8:09am
v3

Do you want to connect to database table without using System.SqlClient namespace. In my opinion not possible. If you will find it, please do tell me then.

or

Just want to bind a datagridview to your customized table (obviously without connecting to SQL) which is easily possible.
 
Share this answer
 
You can bind DataGridView directly to your cutomized table, after retrieving records from database through SqlDataReader instead of DataSet, as below:

DataTable tb2 = new DataTable("Table1");
tb2.Columns.Add("id", System.Type.GetType("System.String"));
tb2.Columns.Add("Dept", System.Type.GetType("System.String"));
cmd = new SqlCommand("Select DeptGroupID, DeptGroup from Department", conn);
SqlDataReader rdr;
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
    DataRow newrow = tb2.NewRow();
    newrow["id"] = rdr.GetInt32(0).ToString();
    newrow["Dept"] = rdr.GetString(1);
    tb2.Rows.Add(newrow);
}
conn.Close();
cmd.Dispose();
dataGridView2.DataSource = tb2.DefaultView;


Hope , it will be useful for you.
 
Share this answer
 
Comments
TugBest 16-May-11 18:13pm    
thank u very very much for this code...but i also want to know if i can display a datagride view without SQL or Access of any data base program....how can this be done
Have a look Here[^]
 
Share this answer
 
You don't have to bind the datagrid to a source. You can add rows and columns dynamically. For example:

this.dataGridView1.Rows.Add(a, b, c);

You can also bind to a data source that is not populated via database.
 
Share this answer
 
Comments
Miroxlav 3-Aug-15 13:04pm    
1. Create necessary columns.
2. Add data rows with the above line of the code
3. To contine working without a dataset, refer to the following properties of your DataGridView:

* Rows to work with records
* Rows.Cells to work with record fields
* Columns to work with column definitions
Short answer, yes, create one dataset programmatically...
 
Share this answer
 
Comments
TugBest 16-May-11 18:15pm    
i want to know if datagride view can be displayed without SQL or ACcess of any database program .. how is it done..


2. how can i also create a dataset programatically.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900