ايمن ميلاد نشر 31 يوليو أرسل تقرير نشر 31 يوليو السلام عليكم عند اضافة صنف لفاتورة موجودة مسبقا لا يتم اضافة صنف نعم يوجد في كود شرط اذا كان صنف موجود مسبقا عدل كمية فقط لكن انا متاكد انه صنف غير موجود اضفت رسالة ظهرت تم اضافة صنف لكن لم يظهر في داتا قريد فيو if (e.KeyCode == Keys.Enter) { try { int quantity = 0; decimal price = 0; if (!int.TryParse(qty.Text, out quantity) || !decimal.TryParse(label5.Text, out price)) { MessageBox.Show("تأكد من صحة إدخال الكمية أو السعر", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } bool itemFound = false; foreach (DataGridViewRow row in buybillForm.dataGridView1.Rows) { if (!row.IsNewRow && row.Cells[5].Value?.ToString() == label11.Text) { // تعديل الصف row.Cells[0].Value = label3.Text; row.Cells[1].Value = label7.Text; row.Cells[2].Value = qty.Text; row.Cells[3].Value = label5.Text; row.Cells[4].Value = quantity * price; // رقم الصنف يبقى كما هو itemFound = true; break; } } // إذا لم يكن موجودًا، تتم إضافته كصف جديد if (!itemFound) { DataRow newRow = buybillForm.BillItemsTable.NewRow(); newRow["item_name"] = label3.Text; newRow["item_date"] = label7.Text; newRow["qty"] = quantity; newRow["price"] = price; newRow["total"] = quantity * price; newRow["item_code"] = label11.Text; buybillForm.BillItemsTable.Rows.Add(newRow); } } catch (Exception ex) { this.Dispose(); MessageBox.Show("حدث خطأ:\n" + ex.Message, "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error); } 1 اقتباس
0 Mustafa Suleiman نشر 31 يوليو أرسل تقرير نشر 31 يوليو المشكلة ليست في إضافة البيانات، بل في عرضها، أي الـ DataGridView لا يقوم بتحديث نفسه تلقائيًا في كل الحالات عند تعديل الـ DataTable المرتبط به مباشرة، أي بحاجة إلى إشعار بأن البيانات قد تغيرت. وأبسط حل هو أن تخبر الـ DataGridView أن يعيد ربط نفسه بمصدر البيانات بعد إضافة الصف الجديد لإجباره على قراءة البيانات من جديد وعرضها. if (!itemFound) { DataRow newRow = buybillForm.BillItemsTable.NewRow(); newRow["item_name"] = label3.Text; newRow["item_date"] = label7.Text; newRow["qty"] = quantity; newRow["price"] = price; newRow["total"] = quantity * price; newRow["item_code"] = label11.Text; buybillForm.BillItemsTable.Rows.Add(newRow); // <<-- هنا -->> buybillForm.dataGridView1.DataSource = buybillForm.BillItemsTable; MessageBox.Show("تمت إضافة الصنف بنجاح"); } فعند تعيين خاصية DataSource مرة أخرى، سيتم إجبار الـ DataGridView على التخلص من العرض القديم للبيانات ورسم نفسه من جديد بناءًا على الحالة الحالية لـ BillItemsTable، والتي تحتوي الآن على الصف الجديد. 1 اقتباس
0 ايمن ميلاد نشر 1 أغسطس الكاتب أرسل تقرير نشر 1 أغسطس لا لزالت تختفي بيانات قديمة موجود في فاتورة ويظهر جديد فقط علي افتراض فاتورة به شوكولاطه وحليب اريد اضافة سكر يظهر سكر ويختفي شوكولاطه وحليب 1 اقتباس
0 Mustafa Suleiman نشر 1 أغسطس أرسل تقرير نشر 1 أغسطس إذن المتغير buybillForm.BillItemsTable الذي تستخدمه لإضافة الصنف الجديد فارغ في كل مرة تبدأ فيها عملية الإضافة، قم بتجربة العمل على نفس نسخة الـ DataTable التي يستخدمها DataGridView كمصدر بيانات منذ البداية. أي يجب أن يكون فورم الإضافة لا ينشئ DataTable جديد، بل يحصل على مرجع للجدول الأصلي الموجود في buybillForm ويعمل عليه مباشرًة. بمعنى عن فتح فورم إضافة الصنف، قم بتمرير BillItemsTable الحالي إليه عبر الـ constructor وهو AddItemForm: AddItemForm addItemForm = new AddItemForm(this.BillItemsTable); addItemForm.ShowDialog(); ثم استقبل الـ DataTable في الـ constructor واحتفظ به في متغير خاص بالفورم. public partial class AddItemForm : Form { private DataTable currentBillItems; private BuyBillForm mainBuyBillForm; public AddItemForm(DataTable billItemsTable, BuyBillForm ownerForm) { InitializeComponent(); this.currentBillItems = billItemsTable; this.mainBuyBillForm = ownerForm; } } ثم استخدم المتغير المحلي currentBillItems بدلاً من buybillForm.BillItemsTable لكي تضمن أنك تضيف الصفوف إلى نفس الجدول المرتبط بالـ DataGridView. if (e.KeyCode == Keys.Enter) { try { int quantity = 0; decimal price = 0; if (!int.TryParse(qty.Text, out quantity) || !decimal.TryParse(label5.Text, out price)) { MessageBox.Show("تأكد من صحة إدخال الكمية أو السعر", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } bool itemFound = false; foreach (DataRow row in this.currentBillItems.Rows) { if (row["item_code"]?.ToString() == label11.Text) { int oldQty = Convert.ToInt32(row["qty"]); row["qty"] = oldQty + quantity; row["total"] = Convert.ToDecimal(row["price"]) * Convert.ToInt32(row["qty"]); itemFound = true; MessageBox.Show("تم تحديث كمية الصنف بنجاح"); this.mainBuyBillForm.UpdateTotals(); break; } } if (!itemFound) { DataRow newRow = this.currentBillItems.NewRow(); newRow["item_name"] = label3.Text; newRow["item_date"] = label7.Text; newRow["qty"] = quantity; newRow["price"] = price; newRow["total"] = quantity * price; newRow["item_code"] = label11.Text; this.currentBillItems.Rows.Add(newRow); // الإضافة تتم على نفس الجدول الأصلي MessageBox.Show("تمت إضافة الصنف بنجاح"); this.mainBuyBillForm.UpdateTotals(); } } catch (Exception ex) { this.Dispose(); MessageBox.Show("حدث خطأ:\n" + ex.Message, "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } اقتباس
0 ايمن ميلاد نشر 3 أغسطس الكاتب أرسل تقرير نشر 3 أغسطس اثناء اضافة صنف using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using System.Globalization; namespace sales { public partial class add_item_to_buybill : Form { private BUYBILL buybillForm; public add_item_to_buybill(BUYBILL formRef) { InitializeComponent(); buybillForm = formRef; } private void qty_TextChanged(object sender, EventArgs e) { } private void qty_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) { e.Handled = true; } } private DataTable currentBillItems; public add_item_to_buybill(DataTable billItemsTable, BUYBILL formRef) { InitializeComponent(); currentBillItems = billItemsTable; buybillForm = formRef; } private void add_item_to_buybill_Load(object sender, EventArgs e) { } private void qty_KeyPress_1(object sender, KeyPressEventArgs e) { } private void qty_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { try { // تعطيل التوليد التلقائي للأعمدة int quantity; decimal price; // ✅ تحقق من صحة القيم أولاً if (!int.TryParse(qty.Text, out quantity) || !decimal.TryParse(label5.Text, out price)) { MessageBox.Show("تأكد من إدخال الكمية والسعر بشكل صحيح", "تنبيه", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } decimal total = quantity * price; // ✅ الإضافة إلى الجدول المرتبط DataRow newRow = currentBillItems.NewRow(); foreach (DataRow row in currentBillItems.Rows) { if (row["item_code"].ToString() == label11.Text) { int oldQty = Convert.ToInt32(row["qty"]); row["qty"] = oldQty + quantity; row["total"] = Convert.ToDecimal(row["price"]) * Convert.ToInt32(row["qty"]); MessageBox.Show("تم تحديث الكمية بنجاح", "معلومة", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } newRow["item_name"] = label3.Text; newRow["item_date"] = label7.Text; newRow["qty"] = quantity; newRow["price"] = price; newRow["total"] = total; newRow["item_code"] = label11.Text; currentBillItems.Rows.Add(newRow); } catch(Exception ex) { MessageBox.Show(ex.Message); } } } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace sales { public partial class BUYBILL : Form { public BUYBILL() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { if (comboBox1_.SelectedIndex < 0) { MessageBox.Show("الرجاء اختيار الزبون", "تنبيه"); comboBox1_.Select(); return; } if (dataGridView1_.Rows.Count == 0) { MessageBox.Show("الرجاء إدخال الأصناف", "تنبيه"); textBox3_.Select(); return; } // 1. التحقق من وجود الفاتورة string sql = "SELECT * FROM BUYBILL WHERE inv_code = @inv_code"; using (SqlDataAdapter adp = new SqlDataAdapter(sql, Class1.sqlCon)) { adp.SelectCommand.Parameters.AddWithValue("@inv_code", inv_code_.Text); DataTable dt = new DataTable(); adp.Fill(dt); if (dt.Rows.Count == 0) { MessageBox.Show("يرجى التحقق من رقم الفاتورة", "تنبيه"); return; } // 2. تحديث بيانات BUYBILL DataRow dr = dt.Rows[0]; dr["customer_name"] = comboBox1_.Text; dr["inv_date"] = DateTime.Now; decimal total = 0; decimal totalQty = 0; foreach (DataGridViewRow row in dataGridView1_.Rows) { if (!row.IsNewRow) { decimal quantity = 0, price = 0; decimal.TryParse(row.Cells[2].Value?.ToString(), out quantity); decimal.TryParse(row.Cells[3].Value?.ToString(), out price); total += quantity * price; totalQty += quantity; } } dr["total"] = total; dr["qty"] = totalQty; using (SqlCommandBuilder cmd = new SqlCommandBuilder(adp)) { adp.Update(dt); } // 3. تحديث حركة الزبون (DET_CUSTOMERS) string sql2 = "SELECT * FROM DET_CUSTOMERS WHERE inv_code = @inv_code"; using (SqlDataAdapter adp3 = new SqlDataAdapter(sql2, Class1.sqlCon)) { adp3.SelectCommand.Parameters.AddWithValue("@inv_code", inv_code_.Text); DataTable dt3 = new DataTable(); adp3.Fill(dt3); DataRow dr2; if (dt3.Rows.Count > 0) dr2 = dt3.Rows[0]; else dr2 = dt3.NewRow(); dr2["inv_code"] = inv_code_.Text; dr2["inv_date"] = DateTime.Now; dr2["debit"] = total; dr2["credit"] = "0"; dr2["cus_name"] = comboBox1_.Text; dr2["cus_code"] = cus_code_.Text; if (dt3.Rows.Count == 0) dt3.Rows.Add(dr2); using (SqlCommandBuilder cmd3 = new SqlCommandBuilder(adp3)) { adp3.Update(dt3); } } string deleteSql = "DELETE FROM BUY_DEL WHERE inv_code = @inv_code"; using (SqlCommand cmdDel = new SqlCommand(deleteSql, Class1.sqlCon)) { cmdDel.Parameters.AddWithValue("@inv_code", inv_code_.Text); cmdDel.ExecuteNonQuery(); } // 5. حفظ تفاصيل الفاتورة الجديدة DataTable dt1 = new DataTable(); SqlDataAdapter adp1 = new SqlDataAdapter("SELECT * FROM BUY_DEL", Class1.sqlCon); adp1.Fill(dt1); foreach (DataGridViewRow row in dataGridView1_.Rows) { if (!row.IsNewRow) { DataRow dr1 = dt1.NewRow(); decimal quantity = 0, price = 0; decimal.TryParse(row.Cells[2].Value?.ToString(), out quantity); decimal.TryParse(row.Cells[3].Value?.ToString(), out price); dr1["inv_code"] = inv_code_.Text; dr1["item_name"] = row.Cells[0].Value?.ToString(); dr1["item_code"] = row.Cells[5].Value?.ToString(); dr1["inv_date"] = DateTime.Now; dr1["item_date"] = row.Cells[1].Value?.ToString(); dr1["qty"] = quantity; dr1["price"] = price; dr1["cus_name"] = comboBox1_.Text; dr1["total"] = quantity * price; dt1.Rows.Add(dr1); // تحديث الكمية في جدول ITEMS string updateQtySql = "UPDATE ITEMS SET item_qty = item_qty - @qty WHERE item_name = @name"; using (SqlCommand cmdQty = new SqlCommand(updateQtySql, Class1.sqlCon)) { cmdQty.Parameters.AddWithValue("@qty", quantity); cmdQty.Parameters.AddWithValue("@name", row.Cells[0].Value?.ToString()); cmdQty.ExecuteNonQuery(); } } } using (SqlCommandBuilder cmd1 = new SqlCommandBuilder(adp1)) { adp1.Update(dt1); } MessageBox.Show("تم تعديل بيانات الفاتورة بنجاح", "رسالة تأكيد", MessageBoxButtons.OK, MessageBoxIcon.Information); button2__Click(sender, e); } } catch (Exception ex) { MessageBox.Show("حدث خطأ أثناء تعديل البيانات:\n" + ex.Message, "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void tabPage3_Click(object sender, EventArgs e) { } private void BUYBILL_Load_1(object sender, EventArgs e) { BillItemsTable = new DataTable(); BillItemsTable.Columns.Add("item_name"); BillItemsTable.Columns.Add("item_date"); BillItemsTable.Columns.Add("qty", typeof(int)); BillItemsTable.Columns.Add("price", typeof(decimal)); BillItemsTable.Columns.Add("total", typeof(decimal)); BillItemsTable.Columns.Add("item_code"); dataGridView1_.AutoGenerateColumns = true; dataGridView1_.DataSource = BillItemsTable; //تعبئة قائمة الاصناف string sql = "SELECT * FROM items"; SqlDataAdapter adp = new SqlDataAdapter(sql, Class1.sqlCon); DataTable dt = new DataTable(); adp.Fill(dt); dataGridView5.AutoGenerateColumns = true; dataGridView5.DataSource = dt; // إخفاء كل الأعمدة عدا item_name foreach (DataGridViewColumn col in dataGridView5.Columns) { if (col.Name != "item_name") col.Visible = false; else col.HeaderText = "اسم الصنف"; } //تعبئة قائمة الفواتير string sql2 = "SELECT inv_code, customer_name, inv_date from buybill "; SqlDataAdapter adp1 = new SqlDataAdapter(sql2, Class1.sqlCon); DataTable dt1 = new DataTable(); adp1.Fill(dt1); dataGridView6.AutoGenerateColumns = true; dataGridView6.DataSource = dt1.DefaultView; dataGridView6.Columns["inv_code"].HeaderText = "رقم الفاتورة"; dataGridView6.Columns["customer_name"].HeaderText = "اسم الزبون"; dataGridView6.Columns["inv_date"].HeaderText = "تاريخ فاتورة"; ////تعبئة اسماء زبائن string query = "SELECT cus_name FROM customers"; DataTable dtt = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(query, Class1.sqlCon); adapter.Fill(dtt); comboBox1_.Items.Clear(); foreach (DataRow row in dtt.Rows) { comboBox1_.Items.Add(row["cus_name"].ToString()); } button2__Click(sender, e); } private void dataGridView5_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void comboBox1__SelectedIndexChanged(object sender, EventArgs e) { string selectedName = comboBox1_.Text.Trim(); // أو comboBox1.SelectedItem.ToString() string sql = $"SELECT cus_code FROM customers WHERE cus_name = @name"; using (SqlCommand cmd = new SqlCommand(sql, Class1.sqlCon)) { cmd.Parameters.AddWithValue("@name", selectedName); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { cus_code_.Text = reader["cus_code"].ToString(); } } } } private void textBox3__TextChanged(object sender, EventArgs e) { try { if (textBox3_.Text.Length != 0) { string sql = "SELECT * FROM items WHERE item_name LIKE @searchTerm"; using (SqlDataAdapter adp = new SqlDataAdapter(sql, Class1.sqlCon)) { adp.SelectCommand.Parameters.AddWithValue("@searchTerm", "%" + textBox3_.Text + "%"); DataTable dt = new DataTable(); adp.Fill(dt); dataGridView5.AutoGenerateColumns = true; dataGridView5.DataSource = dt.DefaultView; } } else { string sql = "SELECT * FROM items"; using (SqlDataAdapter adp = new SqlDataAdapter(sql, Class1.sqlCon)) { DataTable dt = new DataTable(); adp.Fill(dt); dataGridView5.AutoGenerateColumns = true; dataGridView5.DataSource = dt.DefaultView; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "خطا "); } } private void button2__Click(object sender, EventArgs e) { //dataGridView1_.AutoGenerateColumns = false; //// إضافة أعمدة يدوياً بالترتيب المطلوب //dataGridView1_.Columns.Add("item_name", "اسم الصنف"); //dataGridView1_.Columns.Add("item_date", "تاريخ"); //dataGridView1_.Columns.Add("qty", "الكمية"); //dataGridView1_.Columns.Add("price", "السعر"); //dataGridView1_.Columns.Add("total", "الإجمالي"); //dataGridView1_.Columns.Add("item_code", "كود الصنف"); inv_code_.Text = "180000" + (Class1.CODE_GENE("BUYBILL", "ID") + 1).ToString(""); //dataGridView1_.DataSource = null; string query = "SELECT cus_name FROM customers"; DataTable dtt = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(query, Class1.sqlCon); adapter.Fill(dtt); comboBox1_.Items.Clear(); foreach (DataRow row in dtt.Rows) { comboBox1_.Items.Add(row["cus_name"].ToString()); } textBox3_.Select(); cus_code_.Clear(); } private void dataGridView6_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { // استخراج رقم الفاتورة من أول خلية بالصف string invCode_ = dataGridView6.Rows[e.RowIndex].Cells[0].Value.ToString(); inv_code_.Text = invCode_; string sql = @" SELECT d.item_name, d.inv_date, d.qty, d.price, d.total, i.item_code FROM buybill b INNER JOIN BUY_DEL d ON b.inv_code = d.inv_code INNER JOIN ITEMS i ON d.item_name = i.item_name -- أو حسب المفتاح المناسب WHERE b.inv_code = @invCode "; using (SqlDataAdapter adp = new SqlDataAdapter(sql, Class1.sqlCon)) { adp.SelectCommand.Parameters.AddWithValue("@invCode", invCode_); DataTable dt = new DataTable(); adp.Fill(dt); if (dt.Rows.Count > 0) { dataGridView1_.Columns.Clear(); dataGridView1_.AutoGenerateColumns = true; dataGridView1_.DataSource = dt; } else { MessageBox.Show("الفاتورة غير موجودة أو لا تحتوي على أصناف", "تنبيه"); comboBox1_.Text = ""; dataGridView1_.DataSource = null; } } string sqlCus = @" SELECT DISTINCT cus_name FROM BUY_DEL WHERE inv_code = @invCode "; using (SqlDataAdapter adpCus = new SqlDataAdapter(sqlCus, Class1.sqlCon)) { adpCus.SelectCommand.Parameters.AddWithValue("@invCode", invCode_); DataTable dtCus = new DataTable(); adpCus.Fill(dtCus); comboBox1_.Items.Clear(); foreach (DataRow row in dtCus.Rows) { comboBox1_.Items.Add(row["cus_name"].ToString()); } // تحديد أول اسم تلقائيًا (اختياري) if (comboBox1_.Items.Count > 0) comboBox1_.SelectedIndex = 0; else comboBox1_.Text = ""; } } } private void dataGridView1__CellContentClick(object sender, DataGridViewCellEventArgs e) { try { add_item_to_buybill ss = new add_item_to_buybill(this); int selectedRowIndex2 = dataGridView6.CurrentCell.RowIndex; int selectedRowIndex = dataGridView1_.CurrentCell.RowIndex; ss.qty.Text = dataGridView1_.Rows[selectedRowIndex].Cells[2].Value.ToString(); ss.label3.Text = dataGridView1_.Rows[selectedRowIndex].Cells[0].Value.ToString(); ss.label11.Text = dataGridView1_.Rows[selectedRowIndex].Cells[5].Value.ToString(); ss.label7.Text = dataGridView1_.Rows[selectedRowIndex].Cells[1].Value.ToString(); ss.label9.Text = dataGridView1_.Rows[selectedRowIndex2].Cells[4].Value.ToString(); ss.label5.Text = dataGridView1_.Rows[selectedRowIndex].Cells[3].Value.ToString(); ss.ShowDialog(); } catch (Exception ex) { MessageBox.Show("حدث خطأ :\n" + ex.Message, "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private DataTable BillItemsTable; private void dataGridView5_Click(object sender, EventArgs e) { add_item_to_buybill ss = new add_item_to_buybill(this.BillItemsTable, this); int selectedRowIndex = dataGridView5.CurrentCell.RowIndex; ss.label11.Text = dataGridView5.Rows[selectedRowIndex].Cells[1].Value.ToString(); ss.label9.Text = dataGridView5.Rows[selectedRowIndex].Cells[4].Value.ToString(); ss.label3.Text = dataGridView5.Rows[selectedRowIndex].Cells[2].Value.ToString(); ss.label5.Text = dataGridView5.Rows[selectedRowIndex].Cells[5].Value.ToString(); DateTime dateValue = Convert.ToDateTime(dataGridView5.Rows[selectedRowIndex].Cells[3].Value); ss.label7.Text = dateValue.ToString("yyyy-MM-dd"); ss.ShowDialog(); } } } هل هكدا تقصد اقتباس
السؤال
ايمن ميلاد
السلام عليكم عند اضافة صنف لفاتورة موجودة مسبقا لا يتم اضافة صنف
نعم يوجد في كود شرط اذا كان صنف موجود مسبقا عدل كمية فقط لكن انا متاكد انه صنف غير موجود
اضفت رسالة ظهرت تم اضافة صنف لكن لم يظهر في داتا قريد فيو
4 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.