datagrid bind ??????????

Posted by Surajemo under C# on 3/19/2012 | Points: 10 | Views : 1114 | Status : [Member] | Replies : 5
i am trying to bind a datagridview from these two queries select * from customers;select * from employee but i want to bind only first two column from customer and first two coloumn from employee
but only the first two coloumn of customer are getting binded and first two coloumn of employees are not getting binded
her is the code can someone tell me my mistake where am i foing wrong


MySqlConnection con = new MySqlConnection(@"server=localhost;User Id=root;database=classicmodels");
con.Open();

MySqlDataAdapter adp = new MySqlDataAdapter("select * from customers;select * from employee;" ,con);

DataSet ds = new DataSet();

adp.Fill(ds);

DataTable dt = new DataTable();

DataRow dr = null;
DataColumn id = new DataColumn("id", typeof(Int32));
dt.Columns.Add(id);
DataColumn firstname = new DataColumn("Firstname", typeof(String));
dt.Columns.Add(firstname);

DataColumn customerNumber = new DataColumn("customerNumber", typeof(Int32));
dt.Columns.Add(customerNumber);
DataColumn customerName = new DataColumn("customerName", typeof(String));
dt.Columns.Add(customerName);
// DataRow dr1 = null;
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
dr = dt.NewRow();
dr[id] = ds.Tables[0].Rows[i].ItemArray[0];
dr[firstname] = ds.Tables[0].Rows[i].ItemArray[1];
dt.Rows.Add(dr);
}

DataRow dr1 = null;
for (int j = 0; j <= ds.Tables[1].Rows.Count - 1; j++)
{
dr1 = dt.NewRow();
dr1[customerNumber] = ds.Tables[1].Rows[j].ItemArray[0];
dr1[customerName] = ds.Tables[1].Rows[j].ItemArray[1];
dt.Rows.Add(dr1);

}
dataGridView1.DataSource = dt;




Responses

Posted by: Rajni.Shekhar on: 3/20/2012 [Member] Bronze | Points: 25

Up
0
Down
You can change the query...

Select CUST.Col1, CUST.Col2, EMP.Col1,EMP.Col2 from customers CUST, employee EMP

then direct bind to gridview control

Use Following code:

//change col1, col2 as per your columns.
MySqlDataAdapter adp = new MySqlDataAdapter("Select CUST.Col1, CUST.Col2, EMP.Col1,EMP.Col2 from customers CUST, employee EMP;" ,con);

DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataBind();





Thanks,
Rajni Shekhar

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/20/2012 [Member] Silver | Points: 25

Up
0
Down
Hi Surajemo
Do you want to show first two columns of emp and customer in in gridview in same two columns
or you want for emp 2 columns and customer 2 columns?

Thanks,
Sanjay

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Surajemo on: 3/20/2012 [Member] Starter | Points: 25

Up
0
Down
Rajni.Shekhar sir it worked out thanks
Sksamantaray
sir I wanted for emp 2 columns and customer 2 columns

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/20/2012 [Member] Silver | Points: 25

Up
0
Down
I thought your requirement is to show in two columns, had it been a case , you would have used union operator.
Anyway it worked for you.So marked it as answer for Rajani.Shekhar.

Thanks,
Sanjay

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 3/21/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Surajemo,
Good to know, it worked for you, you can mark that as Answer.
And I am not sir :)

Rajni Shekhar


Thanks,
Rajni Shekhar

Surajemo, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response