我想直接给gridview中的某个字段赋值,如:GridView1.Rows[i].Cells[0].Text="1234";
错误提示:索引超出范围。必须为非负值并小于集合大小。
请问该如何实现,最好有详细的过程!
------解决方案--------------------------------------------------------
增加一个模板列
放进去一个TEXTBOX或者LABLE
设置你想要的值
------解决方案--------------------------------------------------------
你确定GridView里有数据吗?你要先绑定数据,再修改
for(int i=0;i<GridView1.Rows.Count;i++)
{
GridView1.Rows[i].Cells[0].Text="1234";
}
------解决方案--------------------------------------------------------
- C# code
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class Test3 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { DataTable dt = DB.getDateTale("select EarningID from EarningListHistory"); GridView1.DataSource = dt; GridView1.DataBind(); for (int i = 0; i < GridView1.Rows.Count; i++) { Label lll = (Label)GridView1.Rows[i].FindControl("EE"); lll.Text = "2222222"; } }}