does not contain a definition for 'DataGridView'

lmrdabendan1 2012-11-07 02:19:08
我用C#做一个WinForm程序时,下面代码的红色部分报如题所示的错误,错误部分代码如下:
private void button2_Click(object sender, EventArgs e)
{
splitContainer1.Panel2.Controls.Clear();
DataGridView ls = new DataGridView();
ls.Location = new Point(89, 53);
ls.Width = 480;
ls.Height = 460;
splitContainer1.Panel2.Controls.Add(ls);//以上,清除Panel2中控件,添加DataGridView控件

string strCoon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TSDRSmartBoard.mdb";
conn = new OleDbConnection(strCoon);
string strSql = "select * from Content_PicTable";
da = new OleDbDataAdapter(strSql, conn);
ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
this.DataGridView.DataSource = dt;
}
求各路高手解答,谢谢。
...全文
175 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
绿领巾童鞋 2012-11-07
  • 打赏
  • 举报
回复
哥们,你这样不行的,DataGridView ls 还是定义为窗体全局变量吧,按钮事件过后可能什么都没有了
lmrdabendan1 2012-11-07
  • 打赏
  • 举报
回复
引用 7 楼 chenandczh 的回复:
ls.DataSource = dt;
解决了,真没想到是this的问题,谢谢
绿领巾童鞋 2012-11-07
  • 打赏
  • 举报
回复
ls.DataSource = dt;
lmrdabendan1 2012-11-07
  • 打赏
  • 举报
回复
引用 3 楼 sj490790083 的回复:
改为this.ls
报同样的错误does not contain a definition for 'ls'
lmrdabendan1 2012-11-07
  • 打赏
  • 举报
回复
引用 2 楼 stiff_neck 的回复:
this.DataGridView.DataSource = dt; 改成 this.ls.DataSource = dt;
报同样的错误does not contain a definition for 'ls'
lmrdabendan1 2012-11-07
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
不存在DataGridView,自己加个控件,改名字为 DataGridView
Panel中有别的控件,所以先清除,然后用代码添加的DataGridview
sj490790083 2012-11-07
  • 打赏
  • 举报
回复
改为this.ls
stiff_neck 2012-11-07
  • 打赏
  • 举报
回复
this.DataGridView.DataSource = dt; 改成 this.ls.DataSource = dt;
bdmh 2012-11-07
  • 打赏
  • 举报
回复
不存在DataGridView,自己加个控件,改名字为 DataGridView

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧