473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataGridView Multiple columns

JustRun
127 100+
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
Sep 30 '09 #1
16 10018
GaryTexmo
1,501 Expert 1GB
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.
Sep 30 '09 #2
JustRun
127 100+
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
Sep 30 '09 #3
GaryTexmo
1,501 Expert 1GB
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.
Sep 30 '09 #4
JustRun
127 100+
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
Sep 30 '09 #5
JustRun
127 100+
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.  
Oct 1 '09 #6
GaryTexmo
1,501 Expert 1GB
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)
Oct 1 '09 #7
JustRun
127 100+
Yeah, Thank you, There is no errors of course :)

Really Thank you, you helped me a lot.
Oct 1 '09 #8
Hi

Sir
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

I am trying open the link you have given is not opening.
it says page not found. please help.

http://members.shaw.ca/gtexmo/Code/C...ColumnDivider/



Thanks
Hasan

@GaryTexmo
Oct 3 '12 #9
GaryTexmo
1,501 Expert 1GB
Interesting coincidence... I checked this forum after a long break, roughly 1 hour after you posted that haha.

I recently changed ISPs so all my links to my old web space are no longer valid. Fortunately I made a backup. Here is an archive with the files you're looking for.
Attached Files
File Type: zip ColumnDivider.zip (3.6 KB, 310 views)
Oct 3 '12 #10
Thanks a lot you have solved my problem

May be it was my lucky day

Just one more Thing how do i use SQLServer data in place of temporary data.

thanks



@GaryTexmo
Oct 3 '12 #11
Just one more Thing how do i use SQLServer data in place of temporary data.

Thanks





Interesting coincidence... I checked this forum after a long break, roughly 1 hour after you posted that haha.

I recently changed ISPs so all my links to my old web space are no longer valid. Fortunately I made a backup. Here is an archive with the files you're looking for.
Oct 4 '12 #12
GaryTexmo
1,501 Expert 1GB
By temporary data, you mean the DataTable I created in the constructor? You should just be able to query your SQLServer and it should return a DataSet for you. I believe there should be a DataAdapter object (probably SQLDataAdapter) that you can set up with a command or connection. Executing it will return a DataSet with the results in it. If you're just doing a simple query, the DataSet will contain a single DataTable (assuming it was successful) and that's all the input you require.

Hopefully that answers your question :)
Oct 4 '12 #13
Thanks

Sorry for bad English.

I did so but I am confuse how do I replace dcValue(dataColumn) with my table Column.


Please have a look to my code. if I run it throws error Column '' does not belong to table.

Please Help




Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. using System.Configuration;
  11.  
  12. namespace RS.Forms
  13. {
  14.     public partial class Form2 : Form
  15.     {
  16.          DataTable dtMain = new DataTable();
  17.          DataColumn dcValue = new DataColumn();
  18.         public Form2()
  19.         {
  20.             InitializeComponent();
  21.  
  22.             string conStr = ConfigurationManager.ConnectionStrings["RS.Properties.Settings.RS2012ConnectionString"].ConnectionString.ToString();
  23.             SqlConnection connection = new SqlConnection(conStr);
  24.             string com = string.Format("SELECT ItemName From Item");
  25.  
  26.             SqlCommand command = new SqlCommand(com, connection);
  27.             connection.Open();
  28.  
  29.             command.CommandType = CommandType.Text;
  30.             DataSet ds = new DataSet();
  31.             command.CommandText = com;
  32.             SqlDataAdapter adapter = new SqlDataAdapter(com, connection);
  33.             adapter.Fill(ds);
  34.  
  35.             dtMain = ds.Tables[0];
  36.  
  37.               for (int i = 0; i < dtMain.Rows.Count; i++)
  38.                 {
  39.                     DataRow dr = dtMain.NewRow();
  40.                     dr[dcValue] = i;
  41.                     dtMain.Rows.Add(dr);
  42.                 }
  43.                 dataGridView1.DataSource = DivideColumn(dtMain, dcValue, 15);
  44.             }  
  45.  
  46.  
  47.  
  48.  
  49.         public DataTable DivideColumn(DataTable source, DataColumn column, int rowsPerColumn)
  50.         {
  51.             DataTable retTable = null;
  52.  
  53.             if (source != null && source.Columns.Contains(column.ColumnName) && source.Rows.Count > rowsPerColumn)
  54.             {
  55.                 string baseColName = column.ColumnName;
  56.                 int numCols = (int)Math.Ceiling((double)source.Rows.Count / (double)rowsPerColumn);
  57.  
  58.                 if (numCols > 0)
  59.                 {
  60.                     retTable = new DataTable();
  61.                     for (int i = 0; i < numCols; i++)
  62.                     {
  63.                         DataColumn newCol = new DataColumn(string.Format("{0}{1}", baseColName, i));
  64.                         retTable.Columns.Add(newCol);
  65.                     }
  66.  
  67.                     // Copy width-wise
  68.                     //for (int i = 0; i < source.Rows.Count; i += numCols)
  69.                     //{
  70.                     //    DataRow dr = retTable.NewRow();
  71.  
  72.                     //    for (int j = 0; j < numCols; j++)
  73.                     //    {
  74.                     //        dr[baseColName + j.ToString()] = dtMain.Rows[i + j][column];
  75.                     //    }
  76.  
  77.                     //    retTable.Rows.Add(dr);
  78.                     //}
  79.  
  80.                     // Copy length-wise
  81.                     for (int i = 0; i < rowsPerColumn; i++)
  82.                     {
  83.                         DataRow dr = retTable.NewRow();
  84.  
  85.                         for (int j = 0; j < numCols; j++)
  86.                         {
  87.                             int sourceCell = i + (j * rowsPerColumn);
  88.                             if (sourceCell < source.Rows.Count)
  89.                             {
  90.                                 dr[baseColName + j.ToString()] = dtMain.Rows[sourceCell][column];
  91.                             }
  92.                         }
  93.  
  94.                         retTable.Rows.Add(dr);
  95.                     }
  96.                 }
  97.             }
  98.  
  99.             return retTable;
  100.         }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.     }
  107. }
  108.  
  109. //-------------------------------------------------------



By temporary data, you mean the DataTable I created in the constructor? You should just be able to query your SQLServer and it should return a DataSet for you. I believe there should be a DataAdapter object (probably SQLDataAdapter) that you can set up with a command or connection. Executing it will return a DataSet with the results in it. If you're just doing a simple query, the DataSet will contain a single DataTable (assuming it was successful) and that's all the input you require.

Hopefully that answers your question :)
Oct 5 '12 #14
Sorry to bother u and for reposting my comment. I did not know that there is holiday in Canada.
I thought you did not receive so I reposted it.

Thanks
Hasan
Oct 9 '12 #15
GaryTexmo
1,501 Expert 1GB
Please don't repost your entire comment just to bump it. It was a holiday weekend in Canada so I wasn't checking the thread.

For your question, please read and understand what the code does. The variable, dcValue, is just the column I created for the purposes of your example. If your data table has a different column you want to split with, use that instead.

You don't need to construct dtMain... your adapter.Fill already gets you the dataset, so just pick the table you want and go from there. I think you can reference columns by names as well (look at DataTable.Columns["COLUMN_NAME"]).
Oct 9 '12 #16
Thanks Alot
Mr. GaryTexmo

Finaly its working its all because of you,
you are great.

Thanks again
Hasan
Oct 11 '12 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brian | last post by:
I've got a couple questions on the new Datagridview control in VS2005. 1) In the old datagrid control at design time I was able to add multiple tablestyles with columns from different...
0
by: Crazy Cat | last post by:
In the following code I retrieve data from a stored procedure that returns multiple resultsets (the results of the stored procedure have been stored in a datareader called reader). On each...
1
by: DBC User | last post by:
Hi All, I am using a database which doesn't suppport and since I am using ORM I can not use join query to create table object mapping. So my question is I have a master table which has bunch...
7
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
0
by: Mike | last post by:
Hey everyone... I've got three problems with a custom DataGridView column I've built following the "How To: Host Controls in Windows Forms DataGridView Cells" article. The base editing control...
3
by: Johnny E. Jensen | last post by:
Hello Dot sure if this it the right group but here goes. I'am using the DataGridView multiple times in my application, and then i'll read a book on inherience, and that opend a new world for...
0
by: LostInMD | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
1
by: Zack00000 | last post by:
Hi, Is there a way to optimize the DataGridView in terms of drawing speed? The application that I'm building will need to use multiple datagridview controls which will get updated around every...
1
by: msgjunkie | last post by:
I have 3 related tables in a DataSet - Customers, Products and Orders. I am trying to create an "editable" DataGridView which, ideally, will contain columns from all the tables. I have tried...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.