首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于combobox绑定数据的有关问题,呀!

2012-07-28 
关于combobox绑定数据的问题,求救呀!!!public class FormOperate{public void InsertCombobox(ComboBox cb

关于combobox绑定数据的问题,求救呀!!!
public class FormOperate
  {
  public void InsertCombobox(ComboBox cb,string name,string table)
  {
  cb.Items.Clear();//清空ComBox  
  string insert = "select"+ name +"from" +table;
  SqlConnection conn = new SqlConnection("server =127.0.0.1;uid = sa; pwd =allen;database =MaterialDataBase");
  SqlCommand cmd = new SqlCommand(insert, conn);//构造连接字符串并切打开数据库连接  
  SqlDataReader read = cmd.ExecuteReader();
  while (read.Read())
  {
  cb.Items.Add(read[0].ToString());//循环读取数据  
  }
  read.Close();// 关闭数据集  
  conn.Close();//关闭数据库连接  
  }
  }

调用FormOperate.InsertCombobox总是报错“非静态的字段、方法或属性“MaterialParametersManagement.FormOperate.InsertCombobox(System.Windows.Forms.ComboBox, string, string)”要求对象引用E:\SJTU\MaterialManagementSystem\MaterialParametersManagement\MaterialParametersManagement\Forms\ParameterBro.cs 43 13 MaterialParametersManagement”


调用语句如下:
FormOperate.InsertCombobox(comboBox1, "ClassNameCN", "MaterialClass");

[解决办法]
静态的方法才是属于类的,非静态的方法要用类的实例去调用。
两种解决方案:
1.public static void InsertCombobox
2.FormOrerate a=new FormOrerate();
a.InsertCombobox;
[解决办法]
FormOperate是类, InsertCombobox如果不是static修士的方法,不能直接通过类名访问,要实例化

FormOrerate f = new FormOrerate();
f.InsertCombobox(xxxx);
[解决办法]
public void InsertCombobox(ComboBox cb,string name,string table)
改成
public static void InsertCombobox(ComboBox cb,string name,string table)
,或者调用时用new FormOperate().InsertCombobox(comboBox1, "ClassNameCN", "MaterialClass");
[解决办法]
直接用cb.DataSource綁定不就好了
cb.Items.Add("test")應該不會有錯的

是不是你的read有問題,你寫的
string insert = "select"+ name +"from" +table;
name,table傳什麼值,傳了值若沒空格,不就是有問題嗎?
至少數據庫中應是select xx from t,而不是selectxxfromt

热点排行