GridView不包含“列”的定义,也没有可以找到接受“GridView”类型的第一个参数的扩展方法“Columns” [英] GridView does not contain a definition for 'Columns' and no extension method 'Columns' accepting a first argument of type 'GridView' could be found

查看:69
本文介绍了GridView不包含“列”的定义,也没有可以找到接受“GridView”类型的第一个参数的扩展方法“Columns”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



***编译器错误消息:CS1061:'GridView'不包含'Columns'的定义,没有可以找到接受GridView类型的第一个参数的扩展方法Columns(你是否缺少using指令或汇编引用?)***



这是cs文件的完整源代码



 使用系统; 
使用 System.Collections.Generic;
// 使用System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Security.Principal;
使用 System.Data.SqlClient;
使用 System.Data;

public partial class 调查:System.Web.UI.Page
{
string responder = WindowsIdentity.GetCurrent()。Name.ToString();
int catID;
int qID;
int canteenID;
string 响应;
string 条评论;
string 评论;
string responseDate = DateTime.Now.ToShortDateString()。ToString();

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



受保护 void Page_Init(对象发​​件人,EventArgs e)
{
// set dataSource properties
sds1.ConnectionString = 数据源= BNY-D-1245;初始目录= canteenSurvey;集成安全=真;
sds1.ProviderName = System.Data.SqlClient;

// sds2.ConnectionString =数据源= BNY-D-1245;初始目录= canteenSurvey; Integrated Security = True;
// sds2.ProviderName =System。 Data.SqlClient;



// 将数据源绑定到页面
sds1.DataBind();
// sds2.DataBind();
}
< span class =code-keyword> protected
void Page_Load( object sender,EventArgs e )
{

}
受保护 void gvCategory_PreRender ( object sender,EventArgs e)
{
foreach (GridViewRow item gvCategory.Rows中的class =code-keyword>
{
catID =( int )gvCategory.DataKeys [item .RowIndex]。价值;

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

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

gvQuestion.Columns [ 0 ]。可见= true ;
gvQuestion.DataSource = sds1;
gvQuestion.DataBind();
gvQuestion.Columns [ 0 ]。可见= false ;

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

}

}

受保护 void btnSubmit_Click( object sender,EventArgs e)
{
foreach (GridViewRow masterItem in gvCategory.Rows)
{
catID =( INT )gvCategory.DataKeys [masterItem.RowIndex]。价值;

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;
// 插入数据库

string constr = 数据源= BNY-D-1245;初始目录= canteenSurvey; Integrated Security = True;

// 用于将值插入数据库的SQL查询

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

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

// SqlCommand query = new SqlCommand( );
使用(SqlConnection dataConnection = new SqlConnection(constr))
{
使用(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();
}

}



}
}
}
}
}





引用:

此代码效果很好在.NET 2.0框架中。不得不将aspx和cs文件复制到.NET 4.0框架,因为我已经实现了图表,并且图表可以用于调查问题。



我也是删除整个文件并在4.0 .NET框架上重新创建,我仍然得到相同的错误。所有的帮助将在解决这个问题时表示赞赏。



谢谢大家。





我在代码的这一部分得到以下错误:



Quote:

gvQuestion .Columns [0] .Visible = true;

gvQuestion.DataSource = sds1;

gvQuestion.DataBind();

gvQuestion.Columns [0] .Visible = false;

解决方案

Quote:



  gvQuestion.Columns [ 0 ]。可见=  true ;  
gvQuestion.DataSource = sds1;
gvQuestion.DataBind();
gvQuestion.Columns [ 0 ]。可见= false ;

第一行会产生问题,因为GridView还没有限制。



下一个问题可能是 - GridView.Columns.Count GridView 中设置 AutoGenerateColumns =true时为0。



如果你想隐藏 GridView 的第一列,那么你也可以在里面尝试RowDataBound event。

  protected   void  gvQuestion_RowDataBound( object  sender,GridViewRowEventArgs e)
{
e.Row.Cells [ 0 ]。可见= false ;
}


I got the above error after copying my aspx & cs files to .NET framework 4.0. as the form was originally developed on .NET framework 2.0. Had to copy it over to framework 4.0 because I needed to use chart controls in the project. I ran the page and got the result

***Compiler Error Message: CS1061: 'GridView' does not contain a definition for 'Columns' and no extension method 'Columns' accepting a first argument of type 'GridView' could be found (are you missing a using directive or an assembly reference?)***

This is the entire source code to the cs file

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();
                        }

                    }



                }
            }
        }        
    }
}



Quote:

This code works well in .NET 2.0 framework. Had to copy the aspx and the cs files to .NET 4.0 framwork because I have implemented chart and the chart is mearnt to work with the survey questions.

I hve also deleted the entire file and recreated same on the 4.0 .NET framework and I still get same error. All help would be appreciated in resolving this issue.

Thanks Guys.



I get the below error in this section of the code:

Quote:

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

解决方案

Quote:


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

The first line would create problem because GridView is not yet bounded.

Next problem might be - GridView.Columns.Count will be 0 when you set AutoGenerateColumns="true" in your GridView.

As you want to hide the first column of GridView, then you can also try inside RowDataBound event.

protected void gvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false;
}


这篇关于GridView不包含“列”的定义,也没有可以找到接受“GridView”类型的第一个参数的扩展方法“Columns”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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