无法更改asp.net上网格视图的列名称 [英] Can't change Column name for grid view on asp.net

查看:81
本文介绍了无法更改asp.net上网格视图的列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个网站,允许用户从类型列表中选择一部电影,然后将该电影选择保存到数据库中.我还添加了一个网格视图控件,该控件允许用户查看选择最多的电影.现在,我已经使该网格视图控件能够正常工作,因为它确实显示了选择最多的影片,但是该列的列名称以及选择了影片的次数具有默认名称列1".代码将其名称更改为"Number",但是这不允许我这样做吗?它显示一条错误消息:"System.ArgumentOutOfRangeException:索引超出范围.必须为非负数,并且小于集合的大小.
参数名称:索引"


这是我使用过的代码,已在代码中标记了错误所在的位置.

Hi, im making a website which allows a user to select a film from a list of genres and then save that film choice to a database. I''ve also added a grid view control which allows the user to view the most selected films. Now i''ve got that grid view control to work as it does display the most selected films, however the column name for the column with the number of times a film has been selected has the default name of Column 1. I added a bit of code to change the name of this to "Number" but it isnt allowing me to do this? It displays an error saying "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"


Heres my code I''ve used, i''ve marked the place in the code where the error is located.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void ButtonDatabase_Click(object sender, EventArgs e)
    {
        // set up data connection
        SqlConnection cs = new SqlConnection("Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True");
        //set up adapter manager 
        SqlDataAdapter da = new SqlDataAdapter();
       
        DataSet ds = new DataSet();
        da.SelectCommand = new SqlCommand("Select FilmName, COUNT(*) FROM Film Group BY FilmName Having COUNT(*) > 1", cs);
        
        
        da.Fill(ds, "Film");
        gvFilms.DataSource = ds;
        gvFilms.DataBind();
 //This is where the error is       gvFilms.Columns[0].HeaderText = "Number";
        btnDatabase.Enabled = false;

        
    }
   }

推荐答案

查询是否返回任何内容?如果返回任何内容,则应始终为列0.

但是,除此之外,您不需要在那里更改列名...您可以在查询中进行操作:

Is the query return anything? There shoudl always be a column 0 if there is anything returned.

But, aside from that, you don''t need to change the column name there...you can do it in the query:

Select FilmName, COUNT(*) as [Number] FROM Film Group BY FilmName Having COUNT(*) > 1


这篇关于无法更改asp.net上网格视图的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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