public bool LoadData(EasyQuery qry, string displayField) { try { bool bLoaded = false; qry.ResetCanChangeDataSet(true); qry.Query(); bLoaded = true; ds = qry.DataStorage; if (ds != null && ds.Tables.Count > 0 && bLoaded) { if (ds.Tables[0].Columns.Count > 0) { int fldIdx = 0; this.DataSource = ds; if (!string.IsNullOrEmpty(displayField)) { for (int i = 0; i < ds.Tables[0].Columns.Count; i++) { if (string.Compare(displayField, ds.Tables[0].Columns[i].ColumnName, StringComparison.OrdinalIgnoreCase) == 0) { fldIdx = i; break; } } } this.DisplayMember = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}.{1}", ds.Tables[0].TableName, ds.Tables[0].Columns[fldIdx].ColumnName); return(true); } } } catch (Exception er) { FormLog.NotifyException(true, er); } return(false); }
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { btDelChild.Enabled = false; cbxFields.Items.Clear(); TableNode nd = treeView1.SelectedNode as TableNode; if (nd != null) { try { int i; btDelChild.Enabled = (nd.Parent != null); // if (!nd.FieldsLoaded) { nd.FieldsLoaded = true; nd.table.GetFields(schema.dbCon); nd.table.GetIndexes(schema.dbCon); } for (i = 0; i < nd.table.FieldCount; i++) { cbxFields.Items.Add(nd.table.GetField(i)); } //check unique index bool bIndexed = false; for (i = 0; i < nd.table.FieldCount; i++) { if (nd.table.GetField(i).Indexed) { bIndexed = true; break; } } if (!bIndexed) { if (nd.table.Indexes != null) { for (i = 0; i < nd.table.Indexes.Length; i++) { if (nd.table.Indexes[i].IsUnique) { for (int j = 0; j < nd.table.Indexes[i].fields.Count; j++) { nd.table.fields[nd.table.Indexes[i].fields[i].Name].Indexed = true; } break; } } } } // dataGrid1.Refresh(); dataGrid1.DataBindings.Clear(); dataGrid1.DataSource = null; dataGrid1.DataMember = null; // qry.ClearQuery(); // dataGrid1.Refresh(); dataGrid1.Name = nd.Text; //create a query for the table qry.UpdatableTableName = nd.Text; qry.Description = nd.Text; qry.SampleTopRec = nd.TopRec; qry.UseSampleTopRec = true; if (qry.DatabaseConnection.TopRecordStyle == EnumTopRecStyle.NotAllowed) { qry.Where = "1=2"; } else { qry.Where = ""; } qry.From = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", qry.NameDelimiterBegin, nd.Text, qry.NameDelimiterEnd); FieldList fl = new FieldList(); for (i = 0; i < nd.table.FieldCount; i++) { fl.AddField(nd.table.GetField(i)); } for (i = 0; i < fl.Count; i++) { fl[i].SetDefaultEditor(); } qry.Fields = fl; qry.Query(); // dataGrid1.DataSource = qry.DataStorage; if (qry.DataStorage != null) { dataGrid1.DataMember = qry.DataStorage.Tables[0].TableName; } dataGrid1.Refresh(); // schema.dbCon = qry.DatabaseConnection.ConnectionObject; dataGrid1_CurrentCellChanged(null, null); dlgDatabase_Resize(null, null); dataGrid1.Visible = (qry.Fields.Count > 0); if (cbxFields.Items.Count > 0) { cbxFields.SelectedIndex = 0; } } catch (Exception err) { MessageBox.Show(this, VPLUtil.FormExceptionText(err), this.Text, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop); } } }