Sunday, September 17, 2017

// // 2 comments

How to merge Gridview header cells C#


In this article I am going to explain How to merge Gridview header cells with C#

As I explained in previous articles bind data with Gridview, If you are new to this article series, please visit How to bind data with Gridview

After bind data Gridview headings shows as below

|    #     |        Name        |     Code    |

I am going to inset an another row above to this headings as below

|  Cheque |                 Bank                  |
----------------------------------------------
|      #       |        Name        |     Code    |


Cheque header column is one cell and Bank header as with 2 cells merged.

Use the below mentioned code inside the RoeDataBound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

 GridViewRow gvr = e.Row;

        if (gvr.RowType == DataControlRowType.Header)
        {
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

            TableCell cell = new TableCell();
            cell.ColumnSpan = 1;
            cell.HorizontalAlign = HorizontalAlign.Center;
            cell.Text = "Cheque";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.ColumnSpan = 2;
            cell.HorizontalAlign = HorizontalAlign.Center;
            cell.Text = "Bank";
            row.Cells.Add(cell);

            GridView1.Controls[0].Controls.AddAt(0, row);
        }
}


Please watch video for more details.




Read More

Monday, September 11, 2017

// // Leave a Comment

How to merge cells in Gridview C#


In this article I am going to explain how to merge cells in Gridview.

If field data is repeating row wise, when we bind data with Gridview, same record is show more times in Gridview as below. 

123456  Commercial Bank PLC
345632  Commercial Bank PLC
378912  Commercial Bank PLC

I am going to show one record for repeating data as below.

123456  
345632  Commercial Bank PLC
378912  

As previous articles we have to drag and drop Griview controller to design form and bind data using data source.



Open the Gridview RoDataBound event and past the below coding


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = GridView1.Rows[rowIndex];
            GridViewRow previousRow = GridView1.Rows[rowIndex + 1];
            if (row.Cells[1].Text == previousRow.Cells[1].Text)
            {
                row.Cells[1].RowSpan = previousRow.Cells[1].RowSpan < 2 ? 2 :
                                             previousRow.Cells[1].RowSpan + 1;
                previousRow.Cells[1].Visible = false;
            }
        }
    }


I have explain this Gridview cell merge with VB also Click hear



Read More

Wednesday, September 6, 2017

// // Leave a Comment

How to merge cells in Gridview VB

In this article I am going to explain how to merge cells in Gridview.

If field data is repeating row wise, when we bind data with Gridview, same record is show more times in Gridview as below. 

123456  Commercial Bank PLC
345632  Commercial Bank PLC
567890  Commercial Bank PLC

I am going to show one record for repeating data as below.

123456  
345632  Commercial Bank PLC
567890  

As previous articles we have to drag and drop Griview controller to design form and bind data using data source.



Open the Gridview RoDataBound event and past the below coding


 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        For rowIndex As Integer = GridView1.Rows.Count - 2 To 0 Step -1

            Dim gviewRow As GridViewRow = GridView1.Rows(rowIndex)

            Dim gviewPreviousRow As GridViewRow = GridView1.Rows(rowIndex + 1)

            For cellCount As Integer = 0 To gviewRow.Cells.Count - 1

                If gviewRow.Cells(cellCount).Text = gviewPreviousRow.Cells(cellCount).Text Then

                    If gviewPreviousRow.Cells(cellCount).RowSpan < 2 Then

                        gviewRow.Cells(cellCount).RowSpan = 2

                    Else

                        gviewRow.Cells(cellCount).RowSpan = gviewPreviousRow.Cells(cellCount).RowSpan + 1

                    End If

                    gviewPreviousRow.Cells(cellCount).Visible = False
                End If

            Next

        Next
    End Sub





Read More