locked
Avoid Repeat In Gridview Row RRS feed

  • Question

  • User2033107836 posted

    Hello

    This is my gridivew look like it has some 10 column for example i have taken two columns first and second

    but i want like this below

    Sunday, April 2, 2017 11:56 AM

Answers

  • User-1509636757 posted
        public partial class WebForm77 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dtSource = GetSampleData();
                    FilterData(dtSource);
                    GridView1.DataSource = dtSource;
                    GridView1.DataBind();
                }
            }
    
            private void FilterData(DataTable dtSource)
            {
                List<Int32> indexListToBlack = new List<Int32>();
                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    string previousDate = string.Empty;
                    string previousNo = string.Empty;
                    string currentDate = string.Empty;
                    string currentNo = string.Empty;
                    if (i == 0)
                        continue;
                    else
                    {
                        previousDate = dtSource.Rows[i - 1]["IssueDate"] as string;
                        previousNo = dtSource.Rows[i - 1]["DcNo"] as string;
    
                        currentDate = dtSource.Rows[i]["IssueDate"] as string;
                        currentNo = dtSource.Rows[i]["DcNo"] as string;
    
                        if (currentDate.Equals(previousDate) && currentNo.Equals(previousNo))
                            indexListToBlack.Add(i);
                    }
                }
                for (int i = 0; i < indexListToBlack.Count; i++)
                {
                    dtSource.Rows[indexListToBlack[i]]["IssueDate"] = string.Empty;
                    dtSource.Rows[indexListToBlack[i]]["DcNo"] = string.Empty;
                }
            }
    
            private DataTable GetSampleData()
            {
                DataTable dtResult = new DataTable();
                dtResult.Columns.AddRange(new DataColumn[] { new DataColumn("IssueDate"), new DataColumn("DcNo") });
    
                DataRow dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "KLSA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "KLSA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "YES";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "YES";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "YES";
                dtResult.Rows.Add(dr);
    
                return dtResult;
            }
    
            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (Convert.ToString((e.Row.DataItem as DataRowView)["IssueDate"]).Equals(string.Empty))
                    {
                        e.Row.Cells[0].ColumnSpan = 2;
                        e.Row.Cells.RemoveAt(1);
                    }
                }
            }
        }
    <asp:GridView 
        runat="server" 
        ID="GridView1" 
        AutoGenerateColumns="false" 
        OnRowCreated="GridView1_RowCreated">
        <Columns>
            <asp:BoundField DataField="IssueDate" HeaderText="Date of Issue" />
            <asp:BoundField DataField="DcNo" HeaderText="No" />
        </Columns>
    </asp:GridView>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 2, 2017 1:52 PM

All replies

  • User-1509636757 posted

    Modify your data source for GridView before assigning to it, and use ColumnSpan in RowCreated or RowDataBound event if underlying DataItem is blank. To modify DataSource, just loop through the data source and check current data row with previous value and if both Date and Dc No matches then modify that data source row with blank value.

    And further as I said, use ColumnSpan in RowCreated or RowDataBound event if underlying DataItem is blank.

    hope that helps./.

    Sunday, April 2, 2017 1:28 PM
  • User2033107836 posted
    Can you give me sample code pls
    Sunday, April 2, 2017 1:38 PM
  • User2033107836 posted

    Modify your data source for GridView before assigning to it, and use ColumnSpan in RowCreated or RowDataBound event if underlying DataItem is blank. To modify DataSource, just loop through the data source and check current data row with previous value and if both Date and Dc No matches then modify that data source row with blank value.

    And further as I said, use ColumnSpan in RowCreated or RowDataBound event if underlying DataItem is blank.

    hope that helps./.

    okay i will try

    Sunday, April 2, 2017 1:50 PM
  • User-1509636757 posted
        public partial class WebForm77 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dtSource = GetSampleData();
                    FilterData(dtSource);
                    GridView1.DataSource = dtSource;
                    GridView1.DataBind();
                }
            }
    
            private void FilterData(DataTable dtSource)
            {
                List<Int32> indexListToBlack = new List<Int32>();
                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    string previousDate = string.Empty;
                    string previousNo = string.Empty;
                    string currentDate = string.Empty;
                    string currentNo = string.Empty;
                    if (i == 0)
                        continue;
                    else
                    {
                        previousDate = dtSource.Rows[i - 1]["IssueDate"] as string;
                        previousNo = dtSource.Rows[i - 1]["DcNo"] as string;
    
                        currentDate = dtSource.Rows[i]["IssueDate"] as string;
                        currentNo = dtSource.Rows[i]["DcNo"] as string;
    
                        if (currentDate.Equals(previousDate) && currentNo.Equals(previousNo))
                            indexListToBlack.Add(i);
                    }
                }
                for (int i = 0; i < indexListToBlack.Count; i++)
                {
                    dtSource.Rows[indexListToBlack[i]]["IssueDate"] = string.Empty;
                    dtSource.Rows[indexListToBlack[i]]["DcNo"] = string.Empty;
                }
            }
    
            private DataTable GetSampleData()
            {
                DataTable dtResult = new DataTable();
                dtResult.Columns.AddRange(new DataColumn[] { new DataColumn("IssueDate"), new DataColumn("DcNo") });
    
                DataRow dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "01-March-2017";
                dr["DcNo"] = "1058";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "KLSA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "02-March-2017";
                dr["DcNo"] = "KLSA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "NA";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "YES";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "YES";
                dtResult.Rows.Add(dr);
    
                dr = dtResult.NewRow();
                dr["IssueDate"] = "03-March-2017";
                dr["DcNo"] = "YES";
                dtResult.Rows.Add(dr);
    
                return dtResult;
            }
    
            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (Convert.ToString((e.Row.DataItem as DataRowView)["IssueDate"]).Equals(string.Empty))
                    {
                        e.Row.Cells[0].ColumnSpan = 2;
                        e.Row.Cells.RemoveAt(1);
                    }
                }
            }
        }
    <asp:GridView 
        runat="server" 
        ID="GridView1" 
        AutoGenerateColumns="false" 
        OnRowCreated="GridView1_RowCreated">
        <Columns>
            <asp:BoundField DataField="IssueDate" HeaderText="Date of Issue" />
            <asp:BoundField DataField="DcNo" HeaderText="No" />
        </Columns>
    </asp:GridView>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 2, 2017 1:52 PM