按钮发送参数给方法

时间:2016-02-19 15:20:35

标签: c# button datagridview

您好我有一些方法从sql表发送数据我想要 - 当我点击datagridview中的按钮时 - 将参数发送到方法和参数将具有ID值(如图107或106所示)。下图是datagridview,带有2个按钮和ID列。

enter image description here

var c = (function() {
  var num = 10;
  var a = function a(n) {
    console.log(n)
    return n * Math.random()
  };
  return a.bind(null, num)
}());

var res = [c(), c(), c()];
console.log(res)

我的课程:

public ObservableCollection<MyClass> ReadUpdate(int id_update)
{
ObservableCollection<MyClass> result = new ObservableCollection<MyClass>();
string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlDataReader dr;
SqlConnection conn = new SqlConnection(nwConn);
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.CommandText = "Insert_Update";
cmd.Parameters.AddWithValue("@id_update", id_update);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
MyClass lin = new MyClass();

lin.id = dr.GetInt32(1);
if (!dr.IsDBNull(2)) lin.other = dr.GetString(2);
if (!dr.IsDBNull(3)) lin.barkod = dr.GetString(3);
if (!dr.IsDBNull(4)) lin.pw = dr.GetInt32(4);

result.Add(lin);
}
dr.Close();
return result;

}
catch (SqlException e)
{
MyClass lin = new MyClass();
lin.other = e.Message;

result.Add(lin);
return result;

}
finally
{
conn.Close();

};
}

我的按钮:

 public class PIS
{
public int ID { get; set; } 
 }

我在webService.ReadUpdateAsync((int)this.DataContext)中有错误; &#34;空引用异常&#34;。

1 个答案:

答案 0 :(得分:1)

在构造函数调用时,您的DataContext尚未设置。如果在构造函数中需要它,则应该将id设置为构造函数参数。

一般来说,您应该使用MVVM和命令模式,您可以在其中将您的id数据指定为XAML中的命令参数。