تنفيذ ميزة ربط بيانات GridDesktop في أوراق العمل
إنشاء نموذج قاعدة بيانات
- قم بإنشاء نموذج قاعدة بيانات لاستخدامها مع المثال. استخدمنا Microsoft Access لإنشاء نموذج قاعدة بيانات مع جدول المنتجات (المخطط أدناه).
- تتم إضافة ثلاثة سجلات وهمية إلى جدول المنتجات. السجلات في جدول المنتجات
قم بإنشاء نموذج تطبيق
الآن قم بإنشاء تطبيق سطح مكتب بسيط في Visual Studio وقم بما يلي.
- اسحب عنصر التحكم “GridControl” من مربع الأدوات وقم بإفلاته في النموذج.
- قم بإسقاط أربعة أزرار من مربع الأدوات أسفل النموذج وقم بتعيين خاصية النص الخاصة بهم على أنهاربط ووكشيت, اضف سطر, احذف صف والتحديث إلى قاعدة البيانات على التوالى.
إضافة مساحة الاسم وإعلان المتغيرات العمومية
لأن هذا المثال يستخدم قاعدة بيانات Microsoft Access ، أضف مساحة الاسم System.Data.OleDb في الجزء العلوي من التعليمات البرمجية.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Adding namespace to the top of code | |
using System.Data.OleDb; |
يمكنك الآن استخدام الفئات التي تم تجميعها ضمن مساحة الاسم هذه.
- قم بتعريف المتغيرات العالمية.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Declaring global variable | |
OleDbDataAdapter adapter; | |
OleDbCommandBuilder cb; | |
DataSet ds; |
تعبئة مجموعة البيانات ببيانات من قاعدة البيانات
اتصل الآن بقاعدة البيانات النموذجية لجلب البيانات وتعبئتها في كائن DataSet.
- استخدم كائن OleDbDataAdapter للاتصال بعينة قاعدة البيانات الخاصة بنا وملء DataSet بالبيانات التي تم جلبها من جدول المنتجات في قاعدة البيانات ، كما هو موضح في الكود أدناه.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
private void DataBindingFeatures_Load(object sender, EventArgs e) | |
{ | |
// The path to the documents directory. | |
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating Select query to fetch data from database | |
string query = "SELECT * FROM Products ORDER BY ProductID"; | |
// Creating connection string to connect with database | |
string conStr = @"Provider=microsoft.jet.oledb.4.0;Data Source=" + dataDir + "dbDatabase.mdb"; | |
// Creating OleDbDataAdapter object that will be responsible to open/close connections with database, fetch data and fill DataSet with data | |
adapter = new OleDbDataAdapter(query, conStr); | |
// Setting MissingSchemaAction to AddWithKey for getting necesssary primary key information of the tables | |
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; | |
/* | |
* Creating OleDbCommandBuilder object to create insert/delete SQL commmands | |
* automatically that are used by OleDbDatAdapter object for updating | |
* changes to the database | |
*/ | |
cb = new OleDbCommandBuilder(adapter); | |
// Creating DataSet object | |
ds = new DataSet(); | |
// Filling DataSet with data fetched by OleDbDataAdapter object | |
adapter.Fill(ds, "Products"); | |
} |
ورقة عمل ملزمة مع DataSet
اربط ورقة العمل بجدول المنتجات في DataSet:
- قم بالوصول إلى ورقة العمل المطلوبة.
- اربط ورقة العمل بجدول منتجات DataSet.
أضف التعليمات البرمجية التالية إلى ملفربط ورقة العمل انقر فوق زر الحدث.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Binding the Worksheet to Products table by calling its DataBind method | |
sheet.DataBind(ds.Tables["Products"], ""); |
تعيين رؤوس الأعمدة لورقة العمل
تقوم ورقة العمل المرتبطة الآن بتحميل البيانات بنجاح ولكن رؤوس الأعمدة يتم تسميتها بشكل افتراضي A و B و C. سيكون من الأفضل تعيين رؤوس الأعمدة لأسماء الأعمدة في جدول قاعدة البيانات.
لتعيين رؤوس أعمدة ورقة العمل:
- احصل على التسميات التوضيحية لكل عمود من DataTable (المنتجات) في DataSet.
- قم بتعيين التسميات التوضيحية إلى رؤوس أعمدة ورقة العمل.
قم بإلحاق الكود المكتوب فيربط ورقة العمل انقر فوق الزر مع مقتطف الشفرة التالي. من خلال القيام بذلك ، سيتم استبدال رؤوس الأعمدة القديمة (A و B و C) بـ ProductID و ProductName و ProductPrice.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Iterating through all columns of the Products table in DataSet | |
for (int i = 0; i < ds.Tables["Products"].Columns.Count; i++) | |
{ | |
// Setting the column header of each column to the column caption of Products table | |
sheet.Columns[i].Header = ds.Tables["Products"].Columns[i].Caption; | |
} |
تخصيص عرض وأنماط الأعمدة
لتحسين مظهر ورقة العمل بشكل أكبر ، من الممكن تعيين عرض وأنماط الأعمدة. على سبيل المثال ، في بعض الأحيان ، يتكون رأس العمود أو القيمة الموجودة داخل العمود من عدد طويل من الأحرف التي لا يمكن احتواؤها داخل الخلية. لحل مثل هذه المشكلات ، يدعم Aspose.Cells.GridDesktop تغيير عرض الأعمدة.
قم بإلحاق التعليمات البرمجية التالية بملفربط ورقة العمل زر. سيتم تخصيص عرض العمود وفقًا للإعدادات الجديدة.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Customizing the widths of columns of the worksheet | |
sheet.Columns[0].Width = 70; | |
sheet.Columns[1].Width = 120; | |
sheet.Columns[2].Width = 80; |
Aspose.Cells. يدعمGridDesktop أيضًا تطبيق الأنماط المهيأة على الأعمدة. الكود التالي ، الملحقة بامتدادربط ورقة العمل الزر ، يخصص أنماط الأعمدة لجعلها أكثر قابلية للتقديم.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Iterating through each column of the worksheet | |
for (int i = 0; i < sheet.ColumnsCount; i++) | |
{ | |
// Getting the style object of each column | |
Style style = sheet.Columns[i].GetStyle(); | |
// Setting the color of each column to Yellow | |
style.Color = Color.Yellow; | |
// Setting the Horizontal Alignment of each column to Centered | |
style.HAlignment = HorizontalAlignmentType.Centred; | |
// Setting the style of column to the updated one | |
sheet.Columns[i].SetStyle(style); | |
} |
الآن قم بتشغيل التطبيق وانقر فوق ملفربط ورقة العمل زر.
مضيفا الصفوف
لإضافة صفوف جديدة إلى ورقة عمل ، استخدم طريقة AddRow لفئة ورقة العمل. يؤدي هذا إلى إلحاق صف فارغ في الجزء السفلي ويتم إضافة DataRow جديد إلى مصدر البيانات (هنا ، تتم إضافة DataRow جديد إلى DataTable الخاص بـ DataSet). يمكن للمطورين إضافة العديد من الصفوف كما يريدون عن طريق استدعاء طريقة AddRow مرارًا وتكرارًا. عند إضافة صف ، يمكن للمستخدمين إدخال القيم فيه.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Adding new row to the worksheet | |
gridDesktop1.GetActiveWorksheet().AddRow(); |
حذف الصفوف
Aspose.Cells.GridDesktop يدعم أيضًا حذف الصفوف عن طريق استدعاء طريقة RemoveRow لفئة ورقة العمل. تتطلب إزالة صف باستخدام Aspose.Cells.GridDesktop حذف فهرس الصف.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Getting the index of the focused row | |
int focusedRowIndex = gridDesktop1.GetActiveWorksheet().GetFocusedCell().Row; | |
// Removing the focused row fro the worksheet | |
gridDesktop1.GetActiveWorksheet().RemoveRow(focusedRowIndex); |
إضافة الكود أعلاه إلىاحذف صف زر وتشغيل التطبيق. يتم عرض عدد قليل من السجلات قبل إزالة الصف. تحديد صف والنقر فوقاحذف صف زر يزيل الصف المحدد.
حفظ التغييرات في قاعدة البيانات
أخيرًا ، لحفظ أي تغييرات أجراها المستخدمون على ورقة العمل مرة أخرى إلى قاعدة البيانات ، استخدم طريقة تحديث كائن OleDbDataAdapter. تأخذ طريقة التحديث مصدر البيانات (DataSet و DataTable وما إلى ذلك) من ورقة العمل لتحديث قاعدة البيانات.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Updating the database according to worksheet data source | |
adapter.Update((DataTable)sheet.DataSource); |
- أضف الكود أعلاه إلىالتحديث إلى قاعدة البيانات زر.
- قم بتشغيل التطبيق.
- قم بإجراء بعض العمليات على بيانات ورقة العمل ، وربما إضافة صفوف جديدة وتحرير البيانات الموجودة أو إزالتها.
- ثم اضغطالتحديث إلى قاعدة البيانات لحفظ التغييرات في قاعدة البيانات.
- تحقق من قاعدة البيانات للتأكد من أنه تم تحديث سجلات الجدول وفقًا لذلك.