GridView不包含“列”的定义,也没有可以找到接受“GridView”类型的第一个参数的扩展方法“Columns”

时间:2014-01-03 09:35:15

标签: c# asp.net gridview

复制我的aspx&后出现上述错误cs文件到.NET framework 4.0。因为表单最初是在.NET framework 2.0上开发的。不得不将其复制到框架4.0,因为我需要在项目中使用图表控件。我运行了页面并得到了结果

  

***编译器错误消息:CS1061:' GridView'不包含'列的定义'没有扩展方法'列'接受类型' GridView'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)***

Source Error:

Line 54: sds1.SelectCommand = "SELECT * FROM tblQuestion WHERE [CatID] = " + catID;
Line 55: 
Line 56: gvQuestion.Columns[0].Visible = true;            
Line 57: gvQuestion.DataSource = sds1;
Line 58: gvQuestion.DataBind();

这是cs文件的完整源代码

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Principal;
using System.Data.SqlClient;
using System.Data;

public partial class Survey : System.Web.UI.Page
{
    string responder = WindowsIdentity.GetCurrent().Name.ToString();
    int catID;
    int qID;
    int canteenID;
    string response;
    string comments;
    string comment;
    string responseDate = DateTime.Now.ToShortDateString().ToString();

    SqlDataSource sds1 = new SqlDataSource();
    //SqlDataSource sds2 = new SqlDataSource();



    protected void Page_Init(object sender, EventArgs e)
    {
        // set dataSource properties
        sds1.ConnectionString = "Data Source=BNY-D-1245;Initial Catalog=canteenSurvey;Integrated Security=True";
        sds1.ProviderName = "System.Data.SqlClient";

        //sds2.ConnectionString = "Data Source=BNY-D-1245;Initial Catalog=canteenSurvey;Integrated Security=True";
        //sds2.ProviderName = "System.Data.SqlClient";



        // bind datasource to page
        sds1.DataBind();
        //sds2.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void gvCategory_PreRender(object sender, EventArgs e)
    {
        foreach (GridViewRow item in gvCategory.Rows)
        {
            catID = (int)gvCategory.DataKeys[item.RowIndex].Value;

            GridView gvQuestion = (GridView)item.FindControl("gvQuestion");

            sds1.SelectCommand = "SELECT * FROM tblQuestion WHERE [CatID] = " + catID;

            gvQuestion.Columns[0].Visible = true;            
            gvQuestion.DataSource = sds1;
            gvQuestion.DataBind();
            gvQuestion.Columns[0].Visible = false;

            foreach (GridViewRow row in gvQuestion.Rows)
            {
                qID = (int)gvQuestion.DataKeys[row.RowIndex].Value;
            }

        }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow masterItem in gvCategory.Rows)
        {
            catID = (int)gvCategory.DataKeys[masterItem.RowIndex].Value;

            GridView gvQuestion = (GridView)masterItem.FindControl("gvQuestion");

            foreach (GridViewRow masterRow in gvQuestion.Rows)
            {
                qID = (int)gvQuestion.DataKeys[masterRow.RowIndex].Value;

                GridView gvCanteen = (GridView)masterRow.FindControl("gvCanteen");

                foreach (GridViewRow masterData in gvCanteen.Rows)
                {
                    canteenID = (int)gvCanteen.DataKeys[masterData.RowIndex].Value;
                    response = ((DropDownList)masterData.FindControl("ddlResponse")).SelectedValue;

                    comments = txtComments.Text;
                    // Insert into the database

                    string constr = "Data Source=BNY-D-1245;Initial Catalog=canteenSurvey;Integrated Security=True";

                    // SQL Query to insert values into the database

                    string sqlQuery = "INSERT INTO tblFeedBack (catID, qID, canteenID, responseID, responder, responseDate)";
                    sqlQuery += "VALUES (@catID, @qID, @canteenID, @responseID, @responder, @responseDate )";

                    //string sqlQuery2 = "INSERT INTO tblComments (responder, comments) VALUES (@responder, @comments)";

                    //SqlCommand query = new SqlCommand();
                    using (SqlConnection dataConnection = new SqlConnection(constr))
                    {
                        using (SqlCommand dataCommand = dataConnection.CreateCommand())
                        {
                            dataConnection.Open();
                            dataCommand.CommandType = CommandType.Text;
                            dataCommand.CommandText = sqlQuery;
                            dataCommand.Parameters.AddWithValue("@catID", catID);
                            dataCommand.Parameters.AddWithValue("@qID", qID);
                            dataCommand.Parameters.AddWithValue("@canteenID", canteenID);
                            dataCommand.Parameters.AddWithValue("@responseID", response);
                            dataCommand.Parameters.AddWithValue("@responder", responder);
                            dataCommand.Parameters.AddWithValue("@responseDate", responseDate);

                            dataCommand.ExecuteNonQuery();
                            dataConnection.Close();
                        }

                    }



                }
            }
        }        
    }
}

我是asp.net的新手,所以我能得到的每一个帮助都会被评估。

2 个答案:

答案 0 :(得分:2)

您收到错误: CS1061

  

当您尝试调用方法或访问类时,会发生此错误   成员不存在。

参考:Compiler Error CS1061

所以我可以说你在复制和粘贴方面犯了一些错误。您应该复制aspx页面的唯一网格内容,而不是复制整个页面。仅复制需要复制的代码。

例如:在Page属性中,您有 CodeFile 继承

的错误值

答案 1 :(得分:1)

尝试使用页面上的CodeFile属性替换CodeBehind属性:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Survey.aspx.cs" MasterPageFile="~/MasterPage.master" Inherits="Survey" %>