예제 #1
0
        private void loadDB()
        {
            string connectionString = $@"Data Source={DBFileName}";

            System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString);
            System.Data.SQLite.SQLiteCommand    cmd  = new System.Data.SQLite.SQLiteCommand("select * from test limit 1");
            cmd.Connection = conn;

            conn.Open();
            cmd.ExecuteScalar();
            System.Data.SQLite.SQLiteDataAdapter da = new System.Data.SQLite.SQLiteDataAdapter(cmd);
            System.Data.DataSet ds = new System.Data.DataSet();

            da.Fill(ds);
            var table = ds.Tables[0];

            minColWidth = new int[table.Columns.Count];
            for (var colIndex = 0; colIndex < minColWidth.Length; colIndex++)
            {
                minColWidth[colIndex] = minWidth;
            }
            foreach (DataRow row in table.Rows)
            {
                for (var colIndex = 0; colIndex < minColWidth.Length; colIndex++)
                {
                    minColWidth[colIndex] = Math.Max(minColWidth[colIndex], TextRenderer.MeasureText(row[colIndex].ToString(), gridControl1.Font).Width);
                }
            }
            ReflectionDictionary dict = new ReflectionDictionary();

            XpoDefault.Dictionary = dict;
            XPClassInfo   classInfo = new XPDataObjectClassInfo(dict, "Test", new Attribute[] { new OptimisticLockingAttribute(false), new DeferredDeletionAttribute(false) });
            List <string> colNames  = new List <string>();

            foreach (DataColumn col in table.Columns)
            {
                colNames.Add(col.ColumnName);
                classInfo.CreateMember(col.ColumnName, col.DataType);
            }
            classInfo.GetMember("id").AddAttribute(new KeyAttribute());

            XPInstantFeedbackSource instantDS = new XPInstantFeedbackSource(classInfo);

            instantDS.ResolveSession += instantDS_ResolveSession;
            instantDS.DismissSession += instantDS_DismissSession;
            gridView.Columns.Clear();
            gridControl1.DataSource = instantDS;
            gridControl1.Refresh();
            btnBestFirColumns.PerformClick();
        }
예제 #2
0
        private static IList <string> GetDynamicClassInfoFromTable(DataTable table, string primaryKeyName, IList <string> displayColumnNames,
                                                                   CaseInsensitiveDictionary <string> filterColumnNamesAndValues, out XPClassInfo classInfo,
                                                                   out ReflectionDictionary reflectionDictionary)
        {
            reflectionDictionary = new ReflectionDictionary();
            classInfo            =
                new XPDataObjectClassInfo(reflectionDictionary.GetClassInfo(typeof(DynamicXpoObject)), table.TableName);
            DataColumn primaryKeyColumn = null;

            if (string.IsNullOrEmpty(primaryKeyName))
            {
                primaryKeyColumn = table.Columns[0];
            }
            else
            {
                foreach (DataColumn col in table.Columns)
                {
                    if (string.Equals(col.ColumnName, primaryKeyName, StringComparison.OrdinalIgnoreCase))
                    {
                        primaryKeyColumn = col;
                        break;
                    }
                }
                if (primaryKeyColumn == null)
                {
                    throw new ArgException("The primary key column named \"{0}\" could not be found in the view \"{1}\"",
                                           primaryKeyName, table.TableName);
                }
            }
            if (CollectionUtils.IsNullOrEmpty(displayColumnNames))
            {
                // Add all columns from the view, ignoring filterColumnNames as displayable, if found
                if (!CollectionUtils.IsNullOrEmpty(filterColumnNamesAndValues))
                {
                    displayColumnNames = new List <string>(table.Columns.Count);
                }
                else
                {
                    displayColumnNames = null;
                }
                bool foundAnyFilterColumns = false;
                foreach (DataColumn col in table.Columns)
                {
                    if (col == primaryKeyColumn)
                    {
                        XPCustomMemberInfo pkMemberInfo = classInfo.CreateMember(col.ColumnName, col.DataType, new KeyAttribute());
                    }
                    else
                    {
                        classInfo.CreateMember(col.ColumnName, col.DataType);
                    }
                    if (displayColumnNames != null)
                    {
                        if (filterColumnNamesAndValues.ContainsKey(col.ColumnName))
                        {
                            foundAnyFilterColumns = true;
                        }
                        else
                        {
                            displayColumnNames.Add(col.ColumnName);
                        }
                    }
                }
                return(foundAnyFilterColumns ? displayColumnNames : null);
            }
            else
            {
                bool          addedPrimaryKeyCol        = false;
                List <string> notFoundFilterColumnNames = null;

                if (!CollectionUtils.IsNullOrEmpty(filterColumnNamesAndValues))
                {
                    notFoundFilterColumnNames = new List <string>(filterColumnNamesAndValues.Keys);
                }

                foreach (string colName in displayColumnNames)
                {
                    DataColumn foundColumn = FindColumn(table, colName);
                    if (foundColumn == null)
                    {
                        throw new ArgException("The display column named \"{0}\" could not be found in the view \"{1}\"",
                                               colName, table.TableName);
                    }
                    if (foundColumn == primaryKeyColumn)
                    {
                        XPCustomMemberInfo pkMemberInfo = classInfo.CreateMember(foundColumn.ColumnName, foundColumn.DataType, new KeyAttribute());
                        addedPrimaryKeyCol = true;
                    }
                    else
                    {
                        classInfo.CreateMember(foundColumn.ColumnName, foundColumn.DataType);
                    }
                    RemoveNameFromList(notFoundFilterColumnNames, foundColumn.ColumnName);
                }
                if (!addedPrimaryKeyCol)
                {
                    classInfo.CreateMember(primaryKeyColumn.ColumnName, primaryKeyColumn.DataType, new KeyAttribute());
                    RemoveNameFromList(notFoundFilterColumnNames, primaryKeyColumn.ColumnName);
                }
                if (!CollectionUtils.IsNullOrEmpty(notFoundFilterColumnNames))
                {
                    for (int i = notFoundFilterColumnNames.Count - 1; i >= 0; --i)
                    {
                        string     colName     = notFoundFilterColumnNames[i];
                        DataColumn foundColumn = FindColumn(table, colName);
                        if (foundColumn != null)
                        {
                            classInfo.CreateMember(foundColumn.ColumnName, foundColumn.DataType);
                            notFoundFilterColumnNames.RemoveAt(i);
                        }
                    }
                }
                return(displayColumnNames);
            }
        }