- 积分
- 13
- 在线时间
- 71 小时
- 主题
- 10
- 注册时间
- 2013-8-12
- 帖子
- 113
- 最后登录
- 2019-2-21
- 帖子
- 113
- 软币
- 647
- 在线时间
- 71 小时
- 注册时间
- 2013-8-12
|
namespace 成果查询分析器.TableView
{
public partial class uc_TableViewByXpoServer : DevExpress.XtraEditors.XtraUserControl
{
public string TableName { get; set; }
public string ConnectionString { get; set; }
public DevExpress.XtraGrid.Views.Grid.GridView MainGridView { get { return gridView1; } }
XPServerCollectionSource XPServerCollectionSource;
public uc_TableViewByXpoServer()
{
InitializeComponent();
}
public uc_TableViewByXpoServer(string _ConnectionString, string _TableName)
{
// TODO: Complete member initialization
this.ConnectionString = _ConnectionString;
this.TableName = _TableName;
InitializeComponent();
lbl_TableName.Text = this.TableName;
}
private void btn_Reload_Click(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
string sMyView = "我的表名";
sMyView = TableName;
ReflectionDictionary dict = new ReflectionDictionary();
// XPClassInfo ciTask = dict.CreateClass(sMyView);
XPClassInfo classInfo = new XPDataObjectClassInfo(dict.GetClassInfo(typeof(LiteDataObject)), sMyView);
// SqlConnection dbConn = new SqlConnection("server=.;Database=Solution2;Integrated Security=True;");
SqlConnection dbConn = new SqlConnection(ConnectionString);
dbConn.Open();
SqlCommand cmd = new SqlCommand(string.Format("Select top 0 * from [{0}]", sMyView), dbConn);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo);
using (DataTable table = new DataTable("mytable"))
{
table.Load(reader);
dbConn.Close();
if (table.PrimaryKey.Count() == 1)
{
DataColumn col = table.PrimaryKey[0];
classInfo.CreateMember(col.ColumnName, col.DataType, new KeyAttribute());
}
else if (table.PrimaryKey.Count() > 1)
{
XPComplexCustomMemberInfo compositeKey = new XPComplexCustomMemberInfo(classInfo, "Key", typeof(object), new Attribute[] { new KeyAttribute(), new BrowsableAttribute(false) });
foreach (DataColumn col in table.PrimaryKey)
{
compositeKey.AddSubMember(col.ColumnName, col.DataType, new Attribute[] { new PersistentAttribute(col.ColumnName), new BrowsableAttribute(false) });
}
}
foreach (DataColumn col in table.Columns)
{
if (classInfo.GetPersistentMember(col.ColumnName) == null)
{
//图形格式的字段不显示
if (col.DataType.Name != "Byte[]")
{
classInfo.CreateMember(col.ColumnName, col.DataType);
}
}
}
}
XpoDefault.DataLayer = XpoDefault.GetDataLayer(this.ConnectionString, dict, DevExpress.Xpo.DB.AutoCreateOption.None);
Session sessionNext = new Session();
XPServerCollectionSource collectionSource = new XPServerCollectionSource(sessionNext, classInfo);
gridControl1.DataSource = null;
gridControl1.DataSource = collectionSource;
gridView1.OptionsView.ColumnAutoWidth = false;
gridView1.BestFitColumns();
if (gridView1.Columns.Count > 0 && gridView1.Columns[0].Summary.Count == 0)
{
gridView1.Columns[0].Summary.Add(DevExpress.Data.SummaryItemType.Count);
}
}
private void btn_SaveToXlsx_Click(object sender, EventArgs e)
{
}
private void uc_TableViewByXpoServer_Load(object sender, EventArgs e)
{
LoadData();
}
private void 保存过滤条件ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFilter();
}
private void saveFilter()
{
DevExpress.Data.Filtering.CriteriaOperator criteria = this.gridView1.ActiveFilterCriteria;
string 过滤器内容=criteria.ToString();
string sql_filterString = DevExpress.Data.Filtering.CriteriaToWhereClauseHelper.GetMsSqlWhere(criteria);
SaveFileDialog sFD = new SaveFileDialog();
sFD.DefaultExt = "HyFilter";
sFD.Filter = "过滤器文件(*.HyFilter)|*.HyFilter";
//文件名自动加上时间标记
sFD.FileName = TableName + "_过滤器 " + DateTime.Now.ToString("yyyy-MM-dd HHmmss");
sFD.OverwritePrompt = true;
if (sFD.ShowDialog() == DialogResult.OK)
{
string saveFileName = sFD.FileName;
if (File.Exists(saveFileName))
{
File.Delete(saveFileName);
}
File.AppendAllText(sFD.FileName, 过滤器内容,Encoding.UTF8);
}
//
}
private void 加载过滤条件ToolStripMenuItem_Click(object sender, EventArgs e)
{
loadFilter();
}
private void loadFilter()
{
OpenFileDialog oFD = new OpenFileDialog();
oFD.DefaultExt = "HyFilter";
oFD.Filter = "过滤器文件(*.HyFilter)|*.HyFilter|过滤器文本(*.HyFilter.txt)|*.HyFilter.txt";
if (oFD.ShowDialog() == DialogResult.OK)
{
string 过滤器内容 = File.ReadAllText(oFD.FileName, Encoding.Default);
this.gridView1.ActiveFilterCriteria = DevExpress.Data.Filtering.CriteriaOperator.Parse(过滤器内容, null);
}
}
private void 设置过滤条件ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.gridView1.ShowFilterEditor(gridView1.Columns[0]);
}
}
[NonPersistent, OptimisticLocking(false)]
public class LiteDataObject : XPBaseObject
{
public LiteDataObject(Session session, XPClassInfo ci) : base(session, ci) { }
public LiteDataObject(Session session) : base(session) { }
}
}
|
评分
-
查看全部评分
|