winform取到数据库信息的方法
我要做的是下图的选择一行商铺点击的详情按钮
private void uiSymbolButton2_Click(object sender, EventArgs e)
{
if (Dgv_Data.SelectedRows.Count != 1)
{
this.ShowWarningDialog("请选中一行!");
return;
}//判断是否选择一行
t_dpxx t = new t_dpxx();
t.Id = int.Parse(Dgv_Data.SelectedRows[0].Cells[0].Value.ToString());//获取你选中表格行的ID
DataTable dt = t.Xian();
t.Shopname = dt.Rows[0][2].ToString();
Xq xq = new Xq(t);
xq.ShowDialog();//显示对话框
}
调用到的Xian()函数
public class t_dpxx
{
public int Id { get; set; }
public string Shopimg { get; set; }
public string Shopname { get; set; }
public string Send { get; set; }
public string Delivery { get; set; }
public string Month { get; set; }
public string Cent { get; set; }
public string Username { get; set; }
public string Idcard { get; set; }
public string Password { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string Mail { get; set; }
public string Brief { get; set; }
//获取数据库中各个列的值
public DataTable Xian()
{
string sql = string.Format("select * from t_dpxx where id ={0}", Id);//数据库中查找我们想获取的数据行
SqlHelper sqlHelper = new SqlHelper();
return sqlHelper.ExecuteReaderEx(sql);
}
}
在详情Xq页面引用
public Xq(t_dpxx t)
{
InitializeComponent();
LabelShopname.Text = t.Shopname;//我们将数值赋值给文本框
LabelId.Text = t.Id.ToString();
}