Click here to Skip to main content
15,916,288 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I made two column:
C#
 GridViewTextBoxColumn txtColumnDay = new GridViewTextBoxColumn();
txtColumnDay.Name = "Days";
txtColumnDay.FieldName = "monthdays";
txtColumnDay.HeaderText = "monthdays";
txtColumnDay.Width = 60;
txtColumnDay.TextAlignment = ContentAlignment.MiddleCenter;
txtColumnDay.AutoSizeMode = BestFitColumnMode.None;
 dataGridView1.MasterTemplate.Columns.Add(txtColumnDay);

 GridViewMaskBoxColumn maskBoxColumnDay = new GridViewMaskBoxColumn();
 maskBoxColumnDay.Name = "Time";
 maskBoxColumnDay.FieldName = "UnitTime";
 maskBoxColumnDay.HeaderText = "Time";
 maskBoxColumnDay.MaskType = MaskType.Standard;
 maskBoxColumnDay.Mask = "00:00";
 maskBoxColumnDay.TextAlignment = ContentAlignment.BottomRight;
 maskBoxColumnDay.FormatString = "{00:00}";
 maskBoxColumnDay.DataType = typeof(TimeSpan);
 maskBoxColumnDay.Width = 60;
 maskBoxColumnDay.TextAlignment = ContentAlignment.MiddleCenter;
 maskBoxColumnDay.AutoSizeMode = BestFitColumnMode.None;
 dataGridView1.MasterTemplate.Columns.Add(maskBoxColumnDay);


now How can I assign my Datatable columns Data to each column?
this is what I did:
C#
DataTable dt = new DataTable();

 dt.Columns.Add("monthdays");
 dt.Columns.Add("UnitTime");
 dt.Rows.Add("monday","00:44");
 dt.Rows.Add("monday","20:44");
 dataGridView1.DataSource = dt;
Posted
Updated 12-Feb-14 0:14am
v4
Comments
Milfje 11-Feb-14 10:54am    
I suggest taking a look at ObjectListView. It'll make your life a hell of a lot easier if you want to do more advanced stuff with DataGrids.
mit62 11-Feb-14 11:40am    
but I need DataGridView :(
Milfje 12-Feb-14 3:37am    
Okay, you probably have your reasons for that :). I don't know the specifics of the problem, but I assume you already defined custom colums (with the posted code), which is why you can't directly bind the datatable to the DataGridView, but want to set the columns individually, correct?

Maybe this link might help you: Bind DataTable to Datagridview that already have column defined into data grid
mit62 12-Feb-14 6:04am    
I did it with regular binding datatable to DGV way and the result was ok.but now I want change my columns to custom maskColumns type.I defined columns and add them to grid then I bind datatable to grid's datasource. but the result is wrong.
Milfje 13-Feb-14 7:56am    
How about keeping it the first way (binding the DataSource/Table and autogenerating the DataGridView) and then customise the columns at runtime (by either their index or name)?

1 solution

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