Click here to Skip to main content
14,930,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a GridView and I need to bind it with a dynamic Data Table.

I have a List<<List<<string>> and a List<<List<<DateTime>>.

I need a DataTable with 3 columns, namely FileName, LastAccessTimeForServer1, LastAccessTimeForServer2.

The List<list><string>> would contain 2 lists of filenames(common for both the servers), while List<list><datetime>> would contain 2 lists of last access times (different for both the files).

Below is the code which I have tried, which doesn't make much sense to me.
Completely perplexed.

C#
List<List<string>> ultimateList=new List<List<string>>();//Its populated by another piece of code
List<List<DateTime>> listOfDateTimes=new List<List<DateTime>>();Its populated by another piece of code
DataTable dtForGridView = new DataTable();
DataColumn dc1 = new DataColumn("File Name", typeof(string));
DataColumn dc2 = new DataColumn("LastAccessTimeForServer1", typeof(DateTime));
DataColumn dc3 = new DataColumn("LastAccessTimeForServer1", typeof(DateTime));
dtForGridView.Columns.Add(dc1);
dtForGridView.Columns.Add(dc2);
dtForGridView.Columns.Add(dc3);


for (int k = 0; k <=2; k++)
{
for (int d = 0; d < numList[k]; d++)//numList[k] will give the number of rows in each column
{
dtForGridView.Rows.Add(ultimateList[d][k], listOfDateTimes[k]);
}
}
//Binding the GridView
GridView1.DataSource = dtForGridView;
GridView1.DataBind();


The final grid should look like:

FileName LastAccessTimeForServer1 LastAccessTimeForServer1
a.txt Time1 Time2
b.txt Time3 Time4

Experts please help in finding a solution.
Any pointers will be highly appreciated.

Regards

Anurag
Posted
Comments
Debabrata_Das 13-Jun-14 7:01am
   
Anurag, Can you tell me why you tried to add two columns with the same name - "LastAccessTimeForServer1" ?
Anurag Sinha V 13-Jun-14 12:11pm
   
Thats a typo mate. Ignore it. The second column name is LastAccessTimeForServer2

1 solution

Hello Anyurag, I tried the following code:

C#
DataTable dtForGridView = new DataTable();
DataColumn dc1 = new DataColumn("File Name", typeof(string));
DataColumn dc2 = new DataColumn("LastAccessTimeForServer1", typeof(DateTime));
dtForGridView.Columns.Add(dc1);
dtForGridView.Columns.Add(dc2);

for (int row = 0; row < ultimateList.Count; row++)
{
    dtForGridView.Rows.Add(ultimateList[row], listOfDateTimes[row]);
}

GridView1.DataSource = dtForGridView;
GridView1.DataBind();

and it's giving me the expected output. Please try.

- DD
   
Comments
Anurag Sinha V 13-Jun-14 8:18am
   
Hi..for 2 columns its gonna gv the correct result..I have around 40 columns for which I need to find a generalized approach..just for simplicity I had given the question with 3 columns.
And there's a typo in the column names..both are different...can u help to find the generalized solution...thxx

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