数据未在gridview中加载。指数超出范围。必须是非负数且小于集合的大小。 [英] Data not loading in gridview. Index was out of range. Must be non-negative and less than the size of the collection.

查看:80
本文介绍了数据未在gridview中加载。指数超出范围。必须是非负数且小于集合的大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.aspx代码



.aspx code

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" EmptyDataText="No records Found">
    
    </asp:GridView>







.cs代码






.cs code

public partial class Pending : System.Web.UI.Page
   {
       SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");

       protected void Page_Load(object sender, EventArgs e)
       {
           BindGridviewData();

       }

       protected void BindGridviewData()
   {
       con.Open();
           string p = "Pending";

           SqlCommand cmd = new SqlCommand("select FirstName,LastName,LocationPreference,College,Status,null as 'Action',null as 'Actions',UserID,InternshipID from UsersApplied where InternshipID='" + Request.QueryString["InternshipID"].ToString() + "' and  Status='" + p + "'", con);

           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
           //DataTable dt = new DataTable();
           //da.Fill(dt);
          // if (dt.Rows.Count > 0)
           if (ds.Tables[0].Rows.Count == 0)
           {
               ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
               GridView1.DataSource = ds;
               GridView1.DataBind();
               int columncount = GridView1.Rows[0].Cells.Count;
               GridView1.Rows[0].Cells.Clear();
               GridView1.Rows[0].Cells.Add(new TableCell());
               GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
               GridView1.Rows[0].Cells[0].Text = "No Records Found";



               //GridView1.DataSource = dt;
               //GridView1.DataBind();


               //Pending();




           }
           else
           {
               GridView1.DataSource = ds;
               GridView1.DataBind();
           }
           con.Close();
   }

推荐答案

我想你想要显示空格子(一个带有状态文本的空白行),这里有几个解决方案



GridView - 在空数据源上显示标题 [ ^ ]
I think you want show the empty Grid(one blank row with status text), here couple of solutions

GridView - Show headers on empty data source[^]


这篇关于数据未在gridview中加载。指数超出范围。必须是非负数且小于集合的大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆