Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing if the next record is the same 1

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
I have a code that works fine to check if the next record is the same and if so then it removes it. my problem is when I try to do this for more than one column. can someone explain to me how this can be done and help me where i can fix the code.

Code:
If e.Row.RowType = DataControlRowType.DataRow Then

            ' Retrieve the underlying data item. In this example
            ' the underlying data item is a DataRowView object. 
            Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)

            '  Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
            If previousCat = drv("FEDPROJECTNUM").ToString() Then
                If gvaddenda.Rows(firstRow).Cells(1).RowSpan = 0 Then
                    gvaddenda.Rows(firstRow).Cells(1).RowSpan = 2
                Else
                    gvaddenda.Rows(firstRow).Cells(1).RowSpan += 1
                End If
                e.Row.Cells.RemoveAt(1)
            Else
                e.Row.VerticalAlign = VerticalAlign.Top
                previousCat = drv("FEDPROJECTNUM").ToString()
                firstRow = e.Row.RowIndex
            End If
        End If

        If e.Row.RowType = DataControlRowType.DataRow Then

            ' Retrieve the underlying data item. In this example
            ' the underlying data item is a DataRowView object. 
            Dim drv As DataRowView = CType(e.Row.DataItem, DataRowView)

            '  Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
            If previousCat = drv("PROPOSAL_ID").ToString() Then
                If gvaddenda.Rows(firstRow).Cells(2).RowSpan = 0 Then
                    gvaddenda.Rows(firstRow).Cells(2).RowSpan = 2
                Else
                    gvaddenda.Rows(firstRow).Cells(2).RowSpan += 1
                End If
                e.Row.Cells.RemoveAt(2)
            Else
                e.Row.VerticalAlign = VerticalAlign.Top
                previousCat = drv("PROPOSAL_ID").ToString()
                firstRow = e.Row.RowIndex
            End If
        End If
 
looks like you are doing this manipulation at the presentation level. I would remove the duplicate rows/columns before I bind the data to the control.
Code:
var tableWithPossibleDuplicates = GetDataTable();
var tableWithDistinctRows = RemoveDuplicatesFrom(tableWithPossibleDuplicates);
MyControl.DataSource = tableWithDistinctRows;
DataBind();
the names of the members above should be self explanatory. all you need to do is implement them.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason thank you for your reply. I am still a little bit confused here. RemoveDuplicatesFrom is this a function that i need to create inorder to remove duplicates? can you clarify your code a little bit further. thank you
 
GetDataTable if a function that returns a datatable. the data could come from anywhere, database, xml, webserivce, etc.

RemoveDuplicatesFrom(DataTable table) does just that. you provide the logic to compare the current record to the previous record. I would code it like this
Code:
private DataTable RemoveDuplicatesFrom(DataTable table)
{
   if(table.Rows.Count < 2) return table;

   var distinct = new DataTable();
   distinct.AddRow(table.Rows[0].ToObjectArray())//or something like that;
   for(var index = 1; index < table.Rows.Count; index++)
   {
       var previous = table.Rows[index-1];
       var current = table.Rows[index];
       var different = //compare previous to current
       if(different)
       {
           distinct.AddRow(current.ToObjectArray());
       }
   }
   return distinct;
}
if you are using a database as the data source, I would remove the duplicates in the sql. if not, then this method works.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
True, I should have stated that if you are using SQL, then do it there.
 
Thank you guys for taking time to help me out here.yes, I am using database as a data source.How could you remove it the duplicated in the sql. I do not think i am clear with my question and I try to clarify that as much as I can. We have job that we let the contractors to bid posted 3 weeks before the letting and when there is some changes in the plan and proposal we post an ammendment on line. the list of ammendment we post will have date of the ammendment, lettingdate, ammendment number, contractno. Let say the contractno, ammendmentposteddateA has two ammendment.
9/12/2009 1 2009 9/14/2009
9/12/2009 2 20009 9/15/2009

I just want this in this format
9 1 2009 9/14/2009
2 9/15/2009

I am not sure how you eliminate the duplicate using sql
 
ok this is a different issue altogether. now we are into grouping data for presentation. sql is not the place to manipulate this.

in all honesty I don't see any value in this approach. you will end up with a grid full of "holes". a grid is for tabular data, and tabular data is predefined columns where each row is unqiue.

if you want to "remove duplicate" data then I would group the data on common columns and display.
[tt]
9-12-09 2009
1 9-14-09
2 9-15-09
[/tt]
however this will change your queries
1. grouped data
2. details of group
and you will need to bind to a dataset instead of table
and you will need to define the relations in the table so you can navigate from the the parent table to the child table for data binding nested databound controls.

the details of that can be found online. i would think there are many tutorials about this from 2001 - 2003 as this was the preferred approach to data access at that time.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I will check out online tutorial.I thank you for your help and spending your valuable time to help me out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top