Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ASP.net code.
Please please help me to select the distinct fields from the procedure output set.
Posted
Updated 4-Mar-13 2:42am
v2
Comments
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..
Am Gayathri 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

i think , it will help you

C#
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;
}
 
Share this answer
 
v2
Comments
Am Gayathri 4-Mar-13 9:39am    
Not working
Am Gayathri 4-Mar-13 9:39am    
Please help me......:(

Kavitha

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900