?
Solved

Is there a way to display a datagridview in columnar format?

Posted on 2008-10-15
10
Medium Priority
?
685 Views
Last Modified: 2013-12-17
I have a c# winform application that will return a recordset of calculated prices certain categories of vehicles. I want to display each row in a columnar format (I've attached a gif that is a mockup of what I'm after)

Is there a relatively easy way to do this?
column-format.gif
0
Comment
Question by:asp316
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 7

Expert Comment

by:the_bachelor
ID: 22725471
You sould be able to do that.
This is how I envision it.
You write a Stored procedure that spits a Resultset with the following columns
Category | 40 | 45 | Super Coach | Sleeper

For the Include checkboxes, you should be able to get thos done by using a gridview footer (wich will put the at the bottow)
I'm unsure if you can use Headers for that....
0
 

Author Comment

by:asp316
ID: 22727864
I don't know how many columns there will be. That's a user definable property of this app.

The datagridview will still display them in a row style of format.
0
 
LVL 7

Expert Comment

by:keustermans
ID: 22740359
Here is some code that will cahange a dataset from horizontal to vertical orientation.

Once you are done you need to make the headers invisible and (Bob's your uncle) the datset has been changed to vertical orientation and it reflects in the datagrid that way.

You need to rember to make the columnheaders invisible as they will have bogus column names.

public DataSet MakeDataSetVerticalDataSet(DataSet switchMe)
{
    DataSet ds = new DataSet();
    foreach(DataTable dt in switchMe.Tables)
    {
        DataTable tempTable = new DataTable();
        for(int i=0; i<=dt.Rows.Count; i++)
        {
            tempTable.Columns.Add(Convert.ToString(i));
        }
        DataRow r;
        for(int k=0; k<dt.Columns.Count; k++)
        {
            r = tempTable.NewRow();
            r[0] = dt.Columns[k].ToString();
            for(int j=1; j<=dt.Rows.Count; j++)
                r[j] = dt.Rows[j-1][k];
        }
        tempTable.Rows.Add(r);
    }
    ds.Tables.Add(tempTable);
}
return ds;
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:asp316
ID: 22754867
That works PERFECTLY! My only question is how do I do the 'Include' checkboxes?
0
 
LVL 7

Expert Comment

by:keustermans
ID: 22755596
As far as I can see you need to include the checkboxe default values  as a row in your original dataset, after switching it you should have the result you require.
0
 

Author Comment

by:asp316
ID: 22757975
It does. It shows true or false. But how do I insert the checkbox control and bind it to the dataset value?
0
 
LVL 7

Expert Comment

by:keustermans
ID: 22758372
Hmmm, let me think about that one.
0
 
LVL 7

Accepted Solution

by:
keustermans earned 2000 total points
ID: 22765150
Here we go.  Put the code below in a function or were ever you do the conversion and you will have the result you want.
 //DataSet switchMe=Fill dataset here
            DataSet ds = MakeDataSetVerticalDataSet(switchMe)
            vertDataGrid.DataSource = null;
            DataGridViewRow newCBrow = new DataGridViewRow();
            try
            {
                int countColumns = 0;
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    if (countColumns > 0)
                    {
                        newCBrow.Cells.Add(new DataGridViewCheckBoxCell());
                        DataGridViewCheckBoxColumn dgcol = new DataGridViewCheckBoxColumn();
 
                        dgcol.HeaderText = "Include in Quote";
 
                        vertDataGrid.Columns.Add(dgcol);
                    }
                    else
                    {
                        newCBrow.Cells.Add(new DataGridViewTextBoxCell());
                        DataGridViewTextBoxColumn dgcol = new DataGridViewTextBoxColumn();
 
                        dgcol.HeaderText = "";
 
                        vertDataGrid.Columns.Add(dgcol);
 
                    }
                    countColumns+=1;
 
                }
           
                vertDataGrid.Rows.Add(newCBrow); 
            }
            catch(Exception)
            {
            }
            try
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    DataGridViewRow newrow = new DataGridViewRow();
                    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                    {
                        DataGridViewTextBoxCell dgt = new DataGridViewTextBoxCell();
                        dgt.Value = row[i].ToString();
                        newrow.Cells.Add(dgt);
                    }
                    vertDataGrid.Rows.Add(newrow);
 
                }
            }
            catch (Exception) { }

Open in new window

0
 
LVL 7

Expert Comment

by:keustermans
ID: 22765157
I have to say that it is not the most elegant way of doing this.  You could override the way the datagrid gets painted but that is a hell of a lot of more work for the same result.
0
 

Author Closing Comment

by:asp316
ID: 31506480
VERY NICE!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question