问题描述
我有一个GridView,我需要用动态数据表绑定它。
我有一个List<< List<< string>>和一个列表<< List<<< DateTime>> ;.
我需要一个包含3列的DataTable,即FileName,LastAccessTimeForServer1,LastAccessTimeForServer2。
列表< list>< string>>将包含2个文件名列表(两个服务器共用),而List< list>< datetime>>将包含2个最后访问时间列表(两个文件都不同)。
下面是我试过的代码,这对我来说没什么意义。
完全困惑。
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.
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();
最终网格应如下所示:
FileName LastAccessTimeForServer1 LastAccessTimeForServer1
a.txt Time1 Time2
b.txt Time3 Time4
专家请帮助查找一个办法。
任何指针都将受到高度赞赏。
问候
Anurag
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
推荐答案
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();
它给了我预期的输出。请试试。
- DD
and it's giving me the expected output. Please try.
- DD
这篇关于通过动态DataTable绑定gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!