Click here to Skip to main content
16,016,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends

the following is my code for binding datatable value to datagridview and in that i have two datatable value 1.period and 2.result. and based on the column and row of these datatble count value i have created the datatable. but i want to set the datagridview column header text from period table and row header text from result datatable. my code is below and i can only able to set the columnName using datatable columnnam property.

period table value is
PeriodNo	StartTime	EndTime
1	        09:00	          10:00
2	        10:01	          11:00
3	        11:10	          12:10
4	        12:11	          01:10
5	        02:01	          03:00
6	        03:01	          04:00
L	        01:11	          02:00

and result table value is
Working_date	working_day
2013-04-22	Monday
2013-04-23	Tuesday
2013-04-25	Thursday
2013-04-26	Friday

and my code is
DataTable result= userdao.getdateanddayfortimetable1(comp);
          DataTable  period = userdao.getperiodfortimetable();
          int periodcount = period.Rows.Count;
          int daycount = result.Rows.Count;
          DataTable dt = new DataTable();
          for (int i = 0; i < periodcount; i++)
          {
              dt.Columns.Add();
          }
          for (int j = 0; j < daycount; j++)
          {
              dt.Rows.Add();
          }
          //System.Windows.Forms.DataGridView grid = new System.Windows.Forms.DataGridView();
          PLayer.GenerateTimetable gt = new PLayer.GenerateTimetable();
         // gt.dataGridView1.DataSource = dt;

          for (int k = 0; k < periodcount; k++)
          {
             //gt.dataGridView1.Columns[k].HeaderText
              dt.Columns[k].ColumnName= period.Rows[k]["PeriodNo"].ToString()+"(" + period.Rows[k]["StartTime"].ToString() +"-"+ period.Rows[k]["EndTime"].ToString() + ")";
          }
          gt.dataGridView1.DataSource = dt;
          for (int m = 0; m < daycount; m++)
          {
              gt.dataGridView1.Rows[m].HeaderCell.Value = result.Rows[m]["Working_day"].ToString();

          }
Posted
Comments
Maciej Los 24-Apr-13 16:07pm    
Not clear. Please, be more specific and provide more details (database engine).
supernorb 26-Apr-13 13:38pm    
The Column HeaderText is easy to set, but the Row HeaderText is something new, I think it's not headerText, it's like header text and it's just the first column of your DataGridView. If so, your job is not too hard to do, the remaining problem is how you fetch your data for each table, it's a query related problem.

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