【问题标题】:Problem with Grid view usage in asp.net C#asp.net C#中的网格视图使用问题
【发布时间】:2011-06-16 06:12:39
【问题描述】:

我想用HyperLink 和其他两个字段填充GridView

我半心半意地尝试了以下方法。它给出了错误。

SqlCommand comm = new SqlCommand(
    @"Select 
        FileUpload.FileName AS FileName,
        FileUpload.FilePath AS PATH,
        SubjectMaster.SubjectName AS Subject,
        MemberPersonalInformation.FirstName As SharedBy 
    from FileUpload 
    INNER JOIN ContentManagement 
        ON ContentManagement.FileId=FileUpload.FileId  
    INNER JOIN MemberPersonalInformation 
        ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy 
    INNER JOIN SubjectMaster 
        ON ContentManagement.SubjectName=SubjectMaster.SubjectId  
    where 
        FileUpload.FileId in
        (
            Select FileId 
            from ContentManagement 
            where 
                ContentId in
                (
                    Select ContentId 
                    from ContentToIndividual 
                    where ShowToMemberId=@MemberId
                ) and 
                ContentManagement.ContentTypeId=@TPD and
                ContentManagement.SessionId=@SessionId
        )", conn);

      conn.Open();
        SqlDataReader rdr = comm.ExecuteReader();
        int i = 0;
        while (rdr.Read())
        {
            string fip = rdr["PATH"].ToString();
            GridViewRow di = GridView1.Rows[i];

                HyperLink h1 = (HyperLink)di.FindControl("Hyperlink1");
                h1.Text = rdr["FileName"].ToString();
                h1.NavigateUrl = "download.aspx?filepath=" + fip;

                di.Cells[1].Text = rdr["SharedBy"].ToString();
                di.Cells[2].Text = rdr["Subject"].ToString();

                i++;

        }
        rdr.Close();

收到错误消息

索引超出范围。一定是 非负且小于 集合。
参数 名称:索引

如果有人建议我更好的方法来执行此操作或更正此错误。

【问题讨论】:

  • 您是否有意尝试使用12,而不是.Cells[0].Cells[1]
  • 抱歉,我想我正在打印Cells[0] 中的超链接。
  • @ILLUMINATI7590:您能否指定出现错误的行..
  • GridViewRow di = GridView1.Rows[i]; 我这里出错了。
  • @ILLUMINATI7590:我认为这里异常的原因是返回的结果集 - 由读者读取 - 具有比 GridView.Rows 集合更多的项目。

标签: c# asp.net gridview templatefield


【解决方案1】:

确保你的gridview至少有三个,否则这行会抛出你描述的异常:

di.Cells[2].Text = rdr["SharedBy"].ToString();

另一方面,您需要小心这一行:

 h1.NavigateUrl = "download.aspx?filepath=" + fip;

将路径传递给客户端然后返回到服务器是引入安全问题的好方法,请确保 download.aspx 检查文件路径参数是否指向真正应该可下载的文件! :)

【讨论】:

  • OP 在GridViewRow di = GridView1.Rows[i]; 行收到错误
  • 实际上,我什至没有到达这条线,程序在GridViewRow di = GridView1.Rows[i];GridViewRow di = GridView1.Rows[i];
  • @Akram - 谢谢,没有阅读整个讨论帖 :-/ 我会把它留在这里以防它仍然有用:)
【解决方案2】:

GridView.Rows 集合索引是从零开始的。你需要用 zero 初始化i

i 变量的值超过GridView.Rows 集合中的可用索引时,会发生错误。因此,如果用 初始化 i 变量并不能解决问题,则返回的结果集 - 由阅读器读取 - 比 GridView.Rows 集合具有更多的项目。

要确保i 变量的值不超过GridView.Rows 集合的可用索引,请使用以下命令:

while (rdr.Read() && i < gridView.Rows.Count)

如果您需要向 GridView 添加行,请使用以下内容:

while (rdr.Read() && i < 3)
{
    string fip = rdr["PATH"].ToString();
    GridViewRow di = new GridViewRow();

    ....

    gridView.Rows.Add(di);        
    i++;        
}

另一个原因可能是您用于引用 GridViewRow.Cells 中的项目的索引。

【讨论】:

  • 我不认为这是问题,它没有改变任何东西。同样的问题仍然存在。
  • 它现在正在跳过整个 while 循环。
  • @ILLUMINATI7590:您需要在网格中添加行吗??
  • 离开我的方法。我得到了四个项目,我想在网格中打印其中的 3 个,其中一个是超链接怎么做?
  • GridViewRow di = new GridViewRow(); 这行给出错误,我们在哪里使用i
【解决方案3】:

设置 int i = 0;并检查,索引从 0 而不是 1 开始

【讨论】:

    【解决方案4】:

    我认为您的读者收藏计数与网格行计数的大小不匹配,这就是为什么它给出“索引超出范围”检查这个 做个检查

    if(GridView1.Rows.Count  > i)
    

    然后在此之后执行您想要的操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 2011-10-20
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      相关资源
      最近更新 更多