Connecting Tech Pros Worldwide Help | Site Map

DataGridView Multiple columns

JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 109
#1: Sep 30 '09
Hi,

suppose I have a table with one column in the database.
If I tried to display this table in a DataGridView it will be displayed in one vertical column.

I need to display this column in 3 or 4 columns in the gridview. but without repeating, just the data as it is in the DB table will be displayed in GV

Any help
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#2: Sep 30 '09

re: DataGridView Multiple columns


Are you saying you want to break that single column in the database up into several columns in a DataGridView?

How do you want to break it up? Every x rows, or by the height of the form? Please let me know if I'm even understanding your problem correctly.
JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 109
#3: Sep 30 '09

re: DataGridView Multiple columns


Yeah, I want to break the single column in the database into several columns in the DataGridView, and I want to break them by the Height of the form

Thanks
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#4: Sep 30 '09

re: DataGridView Multiple columns


Ok cool, that's what I thought but didn't want to assume.

Well, I'd imagine a good way to do this would be to load your data into another DataTable object in the way you want, then display that DataTable in your DGV object. You'll have to do some math to figure out the number of rows you'll want based on the DGV height, as well as update that if you want it to dynamically readjust itself if you resize the form. Also, remember that I believe you can resize the row height of a row in the DataGridView object.

Since the DGV is going to be displaying a different DataTable, you'll probably want to have events set up to reflect changes back into your original DataTable. You'll also need to make sure you're aware of how you map the new row/column representation back to the single column data in order to make sure you update the correct fields.

Does that seem reasonable? There might be a better way to do this... but I don't think there's any "automatic" way with DataGridView. I'll leave you with the general idea for you to try implementing, and if you have any questions, let me know.
JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 109
#5: Sep 30 '09

re: DataGridView Multiple columns


You know, I was afraid to tell me that there is no other way but the DataTable :) Thank you for ur quick reply,
Nice day
JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 109
#6: Oct 1 '09

re: DataGridView Multiple columns


I don't know how to do the select statement to separate the tables it, would u help

Expand|Select|Wrap|Line Numbers
  1. public DataTable GetDataTable()
  2.         {
  3.             int scalar = GetProCount();
  4.             dbModule.InitConnection();
  5.  
  6.             dbModule.cmd = new SqlCommand("SELECT ProName FROM ProductTable", dbModule.con);
  7.             dbModule.adptr = new SqlDataAdapter(dbModule.cmd);
  8.             dbModule.cmb = new SqlCommandBuilder(dbModule.adptr);
  9.  
  10.             DataSet ds = new DataSet();
  11.             DataTable dt = new DataTable();
  12.  
  13.             try
  14.             {
  15.                     dt.Columns.Add("ProName", typeof(string));
  16.                     dt.Columns.Add("الصنـــف2", typeof(string));
  17.                     dt.Columns.Add("الصنـــف3", typeof(string));
  18.                     dt.Columns.Add("الصنـــف4", typeof(string));
  19.                     dt.Columns.Add("الصنـــف5", typeof(string));
  20.  
  21.                 dbModule.adptr.Fill(dt);
  22.             }
  23.             catch (Exception ex)
  24.             {
  25.                 MessageBox.Show(ex.Message.ToString());
  26.             }
  27.             dbModule.DestroyConnection();
  28.             return dt;
  29.         }
  30.  
  31.  
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#7: Oct 1 '09

re: DataGridView Multiple columns


I'd say just do a select statement that gets the whole thing and then divide it up. That way all your data is still stored in a single place that is easy to work with, you only manipulate it for viewing/editing purposes.

I did up a quick example for you... it's basically the solution (I wanted to see if I was actually right, not just feeding you bad theories :P) so if you want to figure it out for yourself, don't look right away. Keep in mind that I'm hard coding the number of columns I'm dividing the source column into... for your solution, you'd pass whatever columns you found to be appropriate based on the height of the form.

http://members.shaw.ca/gtexmo/Code/C...ColumnDivider/
(Note, if you see any coding errors, please feel free to point them out. I did it rather quickly last night so I don't doubt that I may have made a mistake)
JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 109
#8: Oct 1 '09

re: DataGridView Multiple columns


Yeah, Thank you, There is no errors of course :)

Really Thank you, you helped me a lot.
Reply