Solved

MERGE CELLS DATAGRIDVIEW

Posted on 2011-03-22
14
3,256 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now