this.DataGrid1.Columns[1].HeaderText="标题";
我想实现更改列名...可是提示报错
提示:“/myWeb”应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出范围。必须为非负值并小于集合大小。参数名: index 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index

解决方案 »

  1.   

    好的!using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    namespace myWeb
    {
    /// <summary>
    /// userlist 的摘要说明。
    /// </summary> public class userlist : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.LinkButton First;
    protected System.Web.UI.WebControls.LinkButton Previous;
    protected System.Web.UI.WebControls.LinkButton Next;
    protected System.Web.UI.WebControls.LinkButton Last;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.TextBox textPassword;
    protected System.Web.UI.WebControls.TextBox textUser;

    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.TextBox search;
    private string LoginPassword;
    private string LoginNumber;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面 

         //this.Label1.Text=System.Convert.ToString(DataGrid1.PageCount);
            string myConnectionString="Data Source=ghost;Initial Catalog=hwinfo;User Id=sa;Password=1q2w3e";
    SqlConnection conn=new SqlConnection(myConnectionString); 
    conn.Open();
    SqlDataAdapter da=new SqlDataAdapter("select biaoh,name,department,whentime,scsitype,scsiamount,idetype,ideamount from userlist",conn); 
    DataSet ds=new DataSet(); 
    da.Fill(ds,"userlist"); 
    this.DataGrid1.DataSource=ds.Tables["userlist"]; 
    this.DataGrid1.DataBind(); 
                    //SetBind(); 
    First.Click += new System.EventHandler(this.NavigationButtonClick);
    Previous.Click += new System.EventHandler(this.NavigationButtonClick);
    Next.Click += new System.EventHandler(this.NavigationButtonClick);
    Last.Click += new System.EventHandler(this.NavigationButtonClick);
       
        //DropDownList1.Items.Clear();
        SqlCommand cmd=new SqlCommand("select * from search",conn);
        SqlDataReader dr0 = cmd.ExecuteReader();
       //dr0.Read();
      this.DataGrid1.Columns[1].HeaderText="标题";
    if(!Page.IsPostBack)
    {
    try
    {
    DropDownList1.DataSource=dr0;
    DropDownList1.DataTextField="search";
    DropDownList1.DataValueField="search";
    DropDownList1.DataBind();
    }
    //catch()
    //{
    //}
    finally
    {
    dr0.Close();
    conn.Close();
    }

    }
                  }
      

  2.   

    楼主,你要在DG的属性里面手动设置你的colnmu,否则你使用自动绑定的时候,DataGrid1.Columns.Count会为0