Solved

MERGE CELLS DATAGRIDVIEW

Posted on 2011-03-22
14
3,394 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 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