Solved

MERGE CELLS DATAGRIDVIEW

Posted on 2011-03-22
14
3,561 Views
Last Modified: 2012-08-13
Hi All,

I have 4 columns of datatable and using datasource of datagridview to feed data.

Columns Name :

1. InvoiceNo
2. InvoiceDate

3. CustCode
4. CustomerName

The expected display will be:

      Invoice                    Customer
Number    Date               Code      Name

How could I do it ?

Thank you.
0
Comment
Question by:emi_sastra
[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
  • 7
  • 2
  • 2
  • +2
14 Comments
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 300 total points
ID: 35186998
0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 35187009
Create a list from the data table merging columns that you have described.

like
Create a class with 2 properties and create object for the same.
create a list of that class like: List<Class> lstClass = new List<Class>();

convert datatable to list with merging both columns.
and show in the grid.

datagrid.DataSource = lstClass;
datagrid.DataBind();
0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 35187012
This would more help:

 
DataTable dt = CreateDataTableInSomeWay();

List<DataRow> list = new List<DataRow>(); 
foreach (DataRow dr in dt.Rows)
{
    list.Add(dr);
}

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:emi_sastra
ID: 35187180
Hi intStdWorkingDay,

Thank you for the link.

I will try it.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35187185
Hi rikin_shah,

I don't get your point.

Thank you.
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 35187218
>>Hi intStdWorkingDay,
>>Thank you for the link.
>>I will try it

Who is intStdWorkingDay?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35187804
Oops, sorry I mean you.

Thank you.
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35188151
Hi,
You can use this code
<asp:GridView ID="gv" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="Invoice">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <asp:Label ID="lblNumber" runat="server" Text='<%#Eval("InvoiceNo") %>' />
                            </td>
                            <td>
                                <asp:Label ID="lblDate" runat="server" Text='<%#Eval("InvoiceDate") %>' />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Customer">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <asp:Label ID="lblCode" runat="server" Text='<%#Eval("CustCode") %>' />
                            </td>
                            <td>
                                <asp:Label ID="lblName" runat="server" Text='<%#Eval("CustomerName") %>' />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
Furthermore if you don't need specifically, GridView then use can use DataList too in bit related way.
Thanks
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 200 total points
ID: 35194505
You cannot do that using a regular datagridview. You can try to implement your custom datagridview merged cell like this: http://www.codeproject.com/KB/grid/MergedDataGridViewControl.aspx
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35224715
Hi :jpaulino,

--You cannot do that using a regular datagridview
Yes, we can.

Please see the link provided by PagodNaUtak above.

Just using paint and cellpainting events.

I am trying now.

Thank you
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35224721
Hi PagodNaUtak,

I've tried it, but it has not separator between header and detail.

       Invoice                         Customer
Number  |  Date               Code   |   Name

to

      Invoice                         Customer
_______________              ______________

Number  |  Date              Code   |  Name

Could we have header row horizonta separator like column separator ?

Thank you.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 35225715
>> Yes, we can

That's not merging ... is just cell formating (remove the boder). I also do that in my example.

You cannot center text in between two cells or use two cells for the textt, unless you do something like my example shows.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35225844
Please see below code.

What I mean is the header text cell and your sample is for the row cell.

I have test it and it is great.

The problem is convert below display :

       Invoice                         Customer
Number  |  Date               Code   |   Name

to

      Invoice                         Customer
_______________              ______________

Number  |  Date              Code   |  Name


Thank you.

Public Class frmMergeCells

    Private Sub DgvColumnHeaderMerge_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.DataGridView1.Columns.Add("InvNo", "Number")
        Me.DataGridView1.Columns.Add("InvDate", "Date")
        Me.DataGridView1.Columns.Add("CustCode", "Code")
        Me.DataGridView1.Columns.Add("CustName", "Name")

        For intColumn As Integer = 0 To Me.DataGridView1.ColumnCount - 1
            Me.DataGridView1.Columns(intColumn).Width = 45
        Next

        Me.DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        Me.DataGridView1.ColumnHeadersHeight = Me.DataGridView1.ColumnHeadersHeight * 2
        Me.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter

       

    End Sub

    Private Sub dataGridView1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles DataGridView1.Paint
        Dim aryHeader As String() = {"Invoice", "Customer"}

        Dim intColumn As Integer = 0

        While intColumn < DataGridView1.ColumnCount
            'get the column header cell
            Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(intColumn, -1, True)

            r1.X += 1
            r1.Y += 1
            r1.Width = r1.Width * 2 - 2
            r1.Height = r1.Height / 2 - 2

            e.Graphics.FillRectangle(New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1)

            Dim aryFormat As New StringFormat()
            aryFormat.Alignment = StringAlignment.Center
            aryFormat.LineAlignment = StringAlignment.Center

            e.Graphics.DrawString(aryHeader(intColumn \ 2), _
                                  Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, _
                                  New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), _
                                  r1, _
                                  aryFormat)

            intColumn += 2

        End While
    End Sub

    Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
            e.PaintBackground(e.CellBounds, False)

            Dim r2 As Rectangle = e.CellBounds
            r2.Y += e.CellBounds.Height / 2
            r2.Height = e.CellBounds.Height / 2
            e.PaintContent(r2)
            e.Handled = True
        End If
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35406101
Hi All,

Thank you very much for your help.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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