الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة في تلوين الداتاكريدفيو

بدأه realy8 في 1 أكتوبر 2014 · 5 رد · 831 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

 

أخواني لدي dataGridView بأسم DGVTripCustomer ولدي أيضا dataGridView بأسم DGVRoomCustomer

 

أريد القيام بتلوين كل سطر في DGVTripCustomer إذا كانت قيمة العمود الأول فيه تساوي قيمة العمود في DGVTripCustomer 

 

وقد استخدمت هذا الكود واستخدمت RowIndex وقام بتلوين dataGridView بالكامل وليس السطر فقط، واستخدمت value ولم يلون أي سطر

for (int i = 0; i <= DGVTripCustomer.Rows.Count - 1; i++)            {                foreach (DataGridViewRow dgvrow in DGVTripCustomer.Rows)                {                    if (DGVTripCustomer.Rows.Cells[0].RowIndex.ToString() == DGVRoomCustomer.Rows.Cells[2].RowIndex.ToString() &&                         DGVTripCustomer.Rows.Cells[2].RowIndex.ToString() == DGVRoomCustomer.Rows.Cells[4].RowIndex.ToString())                    {                        dgvrow.DefaultCellStyle.BackColor = Color.YellowGreen;                    }                }            }
#2

السلام عليكم اخي الكريم بعد اكتمال القريد الاول اعمل لوب على صفوفه واحفظ قيمة في مصفوفة وفي القريد الثاني في الايفنت الخاص بـ تلوين الخليه وهو DGVTripCustomer _CellFormatting

 

حط الكود التالي 

 private void DGVTripCustomer _CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // Format the RequirdDate and ShippedDate cells differently if the shipped date was passed or near the required date.            
            if (e.ColumnIndex == DGVTripCustomer .Columns[0].Index || e.ColumnIndex == DGVTripCustomer .Columns[1].Index ||
                 e.ColumnIndex == DGVTripCustomer .Columns[5].Index)
            {
                // Get the object value first to check for DBNull since DateTime is a value type.
                object Cust_state = DGVTripCustomer .Rows[e.RowIndex].Cells[5].Value;
                //object Cust_From = DGVTripCustomer .Rows[e.RowIndex].Cells[2].Value;
                Int16 _Cust_state;
                ////int _From;

                if (Cust_state != System.DBNull.Value)
                {
                    try
                    {
                        _Cust_state = (Int16)Cust_state;
                        //_From = (int)Cust_From;
                    }
                    catch (InvalidCastException)
                    {
                        // Either the shipped date or the required date could not be
                        // cast to a DateTime. Don't perform any more formatting of the cell.
                        return;
                    }

                    // Format the cells as red if the Shipped Date was past the Required Date.
                    if (_Cust_state == 0)
                    {
                        
                        e.CellStyle.BackColor = Color.Gray;
                        e.CellStyle.SelectionBackColor = Color.Gray;
                        e.CellStyle.SelectionForeColor = Color.White;
                    }
                    //// Format the cells as yellow if the Shipped Date was within 5 days of the Required Date.
                    //else if (_To < _From)
                    //{
                    //    e.CellStyle.BackColor = Color.PaleGreen;
                    //    e.CellStyle.SelectionBackColor = Color.PaleGreen;
                    //    e.CellStyle.SelectionForeColor = Color.Black;
                    //}
                    ////else
                    ////{

                    ////}

                }
            }
        }

مع مراعاة تغيير الشرط ومقارنته بالقيم في المصفوفه

 

 

اطيب تحياتي 

1
#3

وعليكم السلام عزيز الأخ علي أحمد

 

هل تقصد في اللوب عمل هذا الكود

for (int i = 0; i <= DGVRoomCustomer.Rows.Count - 1; i++)
            {
                data.Rows.AddRange();
            }

وأين أضعه؟

 

وقمت بتعديل الكود الذي أعطيتني إياه إلى التالي فهل ما فعلته صحيح؟

private void DGVTripCustomer_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
           
            if (e.ColumnIndex == DGVTripCustomer .Columns[0].Index && e.ColumnIndex == DGVTripCustomer .Columns[2].Index)
            {
                // Get the object value first to check for DBNull since DateTime is a value type.
                object Cust_state = DGVTripCustomer .Rows[e.RowIndex].Cells[0].Value;
                object Cust_From = DGVTripCustomer .Rows[e.RowIndex].Cells[2].Value;
                Int16 _Cust_state;
                Int16 _From;

                if (Cust_state != System.DBNull.Value && Cust_From != System.DBNull.Value)
                {
                    try
                    {
                        _Cust_state = (Int16)Cust_state;
                        _From = (Int16)Cust_From;
                    }
                    catch (InvalidCastException)
                    {
                        // Either the shipped date or the required date could not be
                        // cast to a DateTime. Don't perform any more formatting of the cell.
                        return;
                    }

                    // Format the cells as red if the Shipped Date was past the Required Date.
                    if (_Cust_state == 0 && _From == 0)
                    {
                        
                        e.CellStyle.BackColor = Color.Gray;
                        e.CellStyle.SelectionBackColor = Color.Gray;
                        e.CellStyle.SelectionForeColor = Color.White;
                    }
              
                    }
            }
#4

كل العذر لكم أخواني على الإزعاج لأنني مبتدئ لا أعلم الكثير

 

سوف أشرح فكرتي لعلي أوصل الفكرة لكم وقد أرفقت صورة للفورم في المرفقات

 

لدي غرف محجوزة أريد تسكين العملاء فيها ووضعت لها DataGirdView لعرض بياناتها،،، وهي على يمين الصورة

ولدي زبائن مسجلين ووضعت لهم DataGirdView لعرض بياناتهم،،، وهي على يسار الصورة

 

ولدي DataGirdView لعرض وتخزين العملاء الذين تم تسكينهم في الغرف،،، وهي في منتصف الصورة

 

=======================

وقمت بعمل الخطوات السابقة بنجاح

=======================

 

وما أريده حاليا هو أن يتغير لو صف كل عميل تم تسكينه

وأيضا تغيير لو صف كل غرفة محجوزة تم التسكين فيها

 

عذرا على الإزعاج، وشكرا لكم على ما تبذلونه في خدمة الناس

 

post-170127-0-06884600-1412231117_thumb.

1
#5
شوف اخي الكريم البرمجة تعتمد في الغالب على المنطق وطيقة معالجة المشكلات

 

في حالتك هذه اعتقد مما قهمته انك تريد تلوين صف اي غرفة محجوزه اذا كان كذلك فأعلم الاتي :

 

من اجل تلوين اي خليه لابد ان يكون في الحدث  CellFormatting

 

وعليه اتوقع بيكون معاك حقل في جدول الغرف يحدد هل الغرفه مسكونة ام لا ؟  اذا في هذا الحدث CellFormatting  استعلم في قاعدة البيانات هل الغرفه محجوزة اذا كانت محجوزه فقم بتغيير اللون

 

يعني يجب ان تعمل حدث CellFormatting خاص بـ العملاء DGVTripCustomer

وحدث CellFormatting آخر خاص بالغرف DGVRoomCustomer

 

وداخل هذه الاحداث تعمل IF لمعرفة حالة الغرفة وتلوينها اذا كانت مسكونه وكذلك حالة العملاء او النزلاء

 

تحياتي

#6

احسنت استاذي علي أحمد لقد وجدت الكود الذي يعمل ذلك وقد نجح معي

// لتلوين الأشخاص المسكنين سابقا
                foreach (DataGridViewRow Myrow in DGVTripCustomer.Rows)
                {
                    if (Myrow.Cells[3].Value.ToString() == DGVRoomCustomer.Rows[e.RowIndex].Cells[7].Value.ToString())
                    {
                        Myrow.Cells[e.ColumnIndex].Style.BackColor = Color.Red;
                    }
                }

وبقي علي أن أغير اللون حسب عدد مرات تكرارها

مواضيع مشابهة