binding data to datagrid
-
in windows apllication, i want to select table's data and show it in datagrid but i dont undertand how will i bind the data is there any way to use datagrd view manager or something like this???? m confused!!!:(
$h@ma!|@
execute SQL statement.....and fill a DataSet with your information if you use ACCESS as a database and VB.NET as your language then follow...
Dim mySql AS String="SELECT * FROM myTable" //Suppose you have a DataAdaptor(da) And DataSet(ds) Dim ds As DataSet Dim da As OleDbDataAdapter da = New OleDbDataAdapter(mySql, myConnection) ds = New DataSet da.Fill(ds) //set DatSource of your datagrid to the dataset DataGrid1.DataSource=ds
Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist
-
execute SQL statement.....and fill a DataSet with your information if you use ACCESS as a database and VB.NET as your language then follow...
Dim mySql AS String="SELECT * FROM myTable" //Suppose you have a DataAdaptor(da) And DataSet(ds) Dim ds As DataSet Dim da As OleDbDataAdapter da = New OleDbDataAdapter(mySql, myConnection) ds = New DataSet da.Fill(ds) //set DatSource of your datagrid to the dataset DataGrid1.DataSource=ds
Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist
well! isnt there any need to use data reader?? well my code is //connection as conn strQuery = " SELECT * FROM table"; SqlCommand com1 = new SqlCommand(strQuery, conn); DataSet ds=new DataSet(); SqlDataAdapter da=new SqlDataAdapter(); SqlDataReader dr = com1.ExecuteReader(); if (dr.Read()) { da.Fill(ds,"table"); } its not working i mean data is not displaying in the grid..and the exception that is shown is "The SelectCommand property has not been initialized before calling 'Fill'."
$h@ma!|@
-
well! isnt there any need to use data reader?? well my code is //connection as conn strQuery = " SELECT * FROM table"; SqlCommand com1 = new SqlCommand(strQuery, conn); DataSet ds=new DataSet(); SqlDataAdapter da=new SqlDataAdapter(); SqlDataReader dr = com1.ExecuteReader(); if (dr.Read()) { da.Fill(ds,"table"); } its not working i mean data is not displaying in the grid..and the exception that is shown is "The SelectCommand property has not been initialized before calling 'Fill'."
$h@ma!|@
why are u using datareader and dataset simultaneously??? follow this...
'1. Create a connection Dim myConnection as New SqlConnection( ConfigurationSettings.AppSettings("connectionString")) '2. Create the command object, passing in the SQL string Const strSQL as String = "SELECT * FROM myTable" Dim myCommand as New SqlCommand(strSQL, myConnection) 'Set the datagrid's datasource to the datareader and databind myConnection.Open() DataGrid1.DataSource = myCommand.ExecuteReader( CommandBehavior.CloseConnection) DataGrid1.DataBind()
but keep in mind A datareader is a read-only object. If you need to edit and update changes you need to use a dataset.Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist
-
why are u using datareader and dataset simultaneously??? follow this...
'1. Create a connection Dim myConnection as New SqlConnection( ConfigurationSettings.AppSettings("connectionString")) '2. Create the command object, passing in the SQL string Const strSQL as String = "SELECT * FROM myTable" Dim myCommand as New SqlCommand(strSQL, myConnection) 'Set the datagrid's datasource to the datareader and databind myConnection.Open() DataGrid1.DataSource = myCommand.ExecuteReader( CommandBehavior.CloseConnection) DataGrid1.DataBind()
but keep in mind A datareader is a read-only object. If you need to edit and update changes you need to use a dataset.Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist
-
hmmmm but as i told i m working in windows application and it doesnt support databind option is there any replacement or any directives/namespaces to include? and what if i want to update records using dataset?
$h@ma!|@
-
sorry shamaila I have forgotten that yours a windows application... then you have to make a
datatable
with that datareader and bind it to the datagrid.... i dont see any other option for you..Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist
-
sorry shamaila I have forgotten that yours a windows application... then you have to make a
datatable
with that datareader and bind it to the datagrid.... i dont see any other option for you..Tirtha Do not go where the path may lead, go instead where there is no path and leave a trail. Author: Ralph Waldo Emerson (1803-82), American writer, philosopher, poet, essayist
Here is a databind for a grid view in windows application: //... SqlConnection conn = new SqlConnection(connectionString); conn.Open(); string strQuery = "SELECT * FROM tableTest"; SqlCommand com1 = new SqlCommand(strQuery, conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(com1); da.Fill(ds, "table"); dataGridView1.DataSource = ds; dataGridView1.DataMember = "table"; //... I hope it works...it worked when I've tested it :)
-
Here is a databind for a grid view in windows application: //... SqlConnection conn = new SqlConnection(connectionString); conn.Open(); string strQuery = "SELECT * FROM tableTest"; SqlCommand com1 = new SqlCommand(strQuery, conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(com1); da.Fill(ds, "table"); dataGridView1.DataSource = ds; dataGridView1.DataMember = "table"; //... I hope it works...it worked when I've tested it :)