Solved

MERGE CELLS DATAGRIDVIEW

Posted on 2011-03-22
14
3,192 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

17 Experts available now in Live!

Get 1:1 Help Now