Solved

MERGE CELLS DATAGRIDVIEW

Posted on 2011-03-22
14
3,330 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
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.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting multiple values from a dataset ASP.NET(VB) 3 49
Convert an ASPX page into PDF 7 32
C# Single Form 8 42
Write to file when application closes (by logoff) - VB.NET 1 31
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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