以编程方式使用列和行RSS创建gridview

时间:2013-08-14 13:39:30

标签: c# gridview rows aspxgridview

我找不到解决问题的帖子。我多次看到Add方法或Insert方法用于添加行但我还没有得到它们!我使用.NET 4.5

我想给你看一个版画,以证明我没有处理这些方法,但我不能因为我太新手而没有这个:/

我需要以编程方式使用一列和多行创建gridview(而不是datagridview)。我手动填充行。如果我理解,我必须这样继续:创建gridview,然后是我添加到gridview的列,然后是我添加到列的行,然后是我添加到行的tablecell。这是好方法吗?

这是我的代码:

    GridView listTypeBot = new GridView();
   // Create my column
   BoundField bf = new BoundField();
   bf.HeaderText = "Types of bot available";
   // Add my column to my gridview
   listTypeBot.Columns.Add(bf);
   // Create a row and a cell
   GridViewRow gvr = listTypeBot.Rows[0];  // I have an exception here : out of range. When I watch my gridview, it does not have row.
   TableCell tc = new TableCell();
   tc.Text = name;
   // Add cell to my row
   gvr.Cells.Add(tc);
   listTypeBot.Rows[0].Cells.Add(tc);

我认为我得到此异常,因为listTypeBot没有任何行。我该如何添加呢?

提前致谢。

1 个答案:

答案 0 :(得分:0)

为什么不只是使用DataSource绑定数据源,数据源是RSS?并将AutoGenerateColumns设置为rue,类似于:

 List<string> names = new List<string>();
 names.Add(" the alghabban  ");
 GridView newGrid = new GridView();                         
 newGrid.DataSource = names;
 newGrid.DataBind(); 

当你需要添加行时,只需将其添加到名称并再次绑定网格视图