单击用户控件上的按钮到datagridview时,我会详细说明 [英] Ho do I the details when clicking button on user control to the datagridview

查看:65
本文介绍了单击用户控件上的按钮到datagridview时,我会详细说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *我正在开发销售点系统。

在我的Cash rejister中,有3个panels.Left面板包括datagrid用于添加transactionProducts,中间一个用于显示类别。我使用用户控件通过单击每个类别来加载这些用户控件。

例如,

当我点击Beverage类别时,它将通过按钮显示其产品来加载BeverageUserControl。(百事可乐,可乐..等)* /



我想做的是用户控件上的点击按钮我希望将它的记录详细信息添加到cashrejister表格上的datagridview



我的尝试:



/*I'm developing Point of Sales system.
In my Cash rejister, there are 3 panels.Left panel include datagrid for add transactionProducts,Middle one displaying categories.I use user controls for right panel.And those user controls load by clicking each categories.
For example,
When I click Beverage category it will load BeverageUserControl by showing its product by buttons.(Pepsi, Coke..etc)*/

What i want to do is while click buttons on user control i want add it's record details to datagridview on cashrejister Form

What I have tried:

public partial class Food : UserControl
	{
		public Food()
		{
			InitializeComponent();
		}
		static string myconn = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
		private void btnNasi_Click(object sender, EventArgs e)
		{
			ProductController p = new ProductController();
			SqlConnection con = new SqlConnection(myconn);
			DataTable dt = new DataTable();

			try
			{   //I think this is not practical. cz i have to write like this to every button
				string sql = "SELECT name,rate,qty FROM tbl_product WHERE name='Nasi Goreng'";
				SqlDataAdapter adt = new SqlDataAdapter(sql,con);
				con.Open();
				adt.Fill(dt);
				if (dt.Rows.Count>0)
				{
					p.name = dt.Rows[0]["name"].ToString();
					p.rate = decimal.Parse(dt.Rows[0]["rate"].ToString());
					p.qty = decimal.Parse(dt.Rows[0]["qty"].ToString());
				}

			}
			catch (Exception)
			{

				throw;
			}
			DataTable transactionDT = new DataTable();
			decimal total = p.rate * p.qty;
			transactionDT.Rows.Add(p.name, p.rate, p.qty, total);

//the datagrid is on the cashrejister form.
//I want to load transactionDT on to that datagridview.

		}
	}

推荐答案

(嗯,Nasi Goreng。有些虾片和沙爹请!)



将数据网格的数据源设置为现有的空集合;然后将您的订单信息添加到该集合。



如果是WPF,您将使用ObservableCollection,当对集合进行更新时,它会自动引用网格(可以使用公共引用从其他表单访问)。



您当前正在尝试创建新数据表并将其用于数据网格,导致阻抗不匹配(缺少更好的术语)。
(Hmmmm, Nasi Goreng. Some shrimp chips and satay please!)

Set the data source of the data grid to an existing "empty" collection; then add your order information to that collection.

If it was WPF, you would use an ObservableCollection which would automatically "referesh" the grid as updates are made to the collection (which can be accessed from another form using a public reference).

You're currently trying to create a "new" data table and using that for the data grid, which is causing an impedance mismatch (for lack of a better term).


这篇关于单击用户控件上的按钮到datagridview时,我会详细说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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