Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,

The below code is for setting column header for a new datatable.one time it is worked.but in second time The same column header appeared in the datatable. so it makes error. I want to refresh the table every time i executed this method. how can delete the column header.

C#
public DataTable setgrid(DataTable studlist,DataTable subjectlist,business.Commonpropertis comp)
        {
            string marktype1 = "Internal Mark";
            string marktype2 = "External Mark";
            string divide="|";
            DataTable grid = null;
            grid = new DataTable();
           
            for (int i = 0; i < studlist.Columns.Count; i++)
            {
                grid.Columns.Add(studlist.Columns["AdmissionNumber"].ColumnName.ToString());
                grid.Columns.Add(studlist.Columns["RollNumber"].ColumnName.ToString());
                grid.Columns.Add(studlist.Columns["Name"].ColumnName.ToString());

            }
Posted
Comments
ZurdoDev 1-Oct-13 8:13am    
You are already creating a new DataTable each time setgrid is called. Your problem is you are looping and adding columns. Why are you doing that?
baskaran chellasamy 1-Oct-13 8:31am    
because whenever execute this method i add extra column based on the dynamic situation. but this three column are fixed. in a second time i execute the this method it return an error that specify Above three column is added already.so that i need to refresh this table in every execution.

1 solution

You don't need a loop just create the columns once.
 
Share this answer
 
Comments
baskaran chellasamy 1-Oct-13 8:35am    
then how do i skip the loop after first time. i dont know how to skip, because it is the column header name.
[no name] 1-Oct-13 8:40am    
break;
baskaran chellasamy 1-Oct-13 8:45am    
In a second parameter subjectlist will be changing dynamically. so i need the add the subjectlist for the column header dynamically. for that i need to refresh the datatable based on the situation. i think it is better every time when i execute method i want to refresh datatable and also delete the column name is also.

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