how to bind an array of data to a datagrid?

  • Thread starter Thread starter irnbru irnbru
  • Start date Start date
I

irnbru irnbru

Hi all! I got an array of datarows after a select on a datatable, I want
to show the results in datagrid, but it doesnt work, do i have to create
a dataview? Please help me out thank you IRNBRU
 
Hi Irnbru,

You should use DataView. here is some sample code for you.

SqlCommand cmd = new SqlCommand();

SqlConnection con = new SqlConnection();

DataSet ds = new DataSet();


SqlDataAdapter adap = new SqlDataAdapter();

SqlDataReader rdr ;


con.ConnectionString = @"server=.;database=AdventureWorks;uid=sa;pwd=sa";

con.Open();

cmd.Connection = con;

cmd.CommandType = CommandType.Text;

cmd.CommandText = @"select LocationID,Name as LocNAme from
Production.Location";

adap.SelectCommand = cmd;

adap.Fill(ds, "ER");

DataRow[] dr = ds.Tables[0].Select();


dataGrid1.DataSource = dr[0].Table.DefaultView;



vinu
 
Back
Top