Click here to Skip to main content
13,043,154 members (61,762 online)
Rate this:
Please Sign up or sign in to vote.
I want to generate columns in a grid dynamically, i used grid.addcolumn property but
My code is throwing error like "Column name already exists". The error is because the procedure which am using
in database returning same values. I can't use 'distinct' in my procedure because the procedure returning more than one field and only
the field which i need to show as a dynamic column is repeating.
So i need to do one filtering in code.
Please please help me to select the distinct fields from the procedure output set.
Posted 4-Mar-13 1:41am
Updated 4-Mar-13 2:42am
willington.d 4-Mar-13 7:47am
Could you please post the code? So that we can understand more.
vishal.shimpi 4-Mar-13 8:35am
what u want to do? u are retrieving row values from database and creating the column of that name..
Kavitha le 4-Mar-13 8:41am
Am not suppose to post the code because of some security reasons.
Here i need to add columns in a grid dynamically.Column names we are fetching using one procedure.But that procedure returning duplicate values so am getting error while adding same columns.
So i need to filter the data retrieved by the procedure. I need distinct values.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

i think , it will help you

public DataTable SelectDistinct(DataTable SourceTable, string FieldName)
    // Create a Datatable  datatype same as FieldName
    DataTable dt = new DataTable(SourceTable.TableName);
    dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
    // Loop each row & compare each value with one another
    // Add it to datatable if the values are mismatch
    object LastValue = null;
    foreach (DataRow dr in SourceTable.Select("", FieldName))
        if (LastValue == null || !(ColumnEqual(LastValue,dr[FieldName])))
            LastValue = dr[FieldName];
           dt.Rows.Add(new object[] { LastValue });
    return dt;
Kavitha le 4-Mar-13 9:39am
Not working
Kavitha le 4-Mar-13 9:39am
Please help me......:(


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100