Exemple #1
0
 public static void Bind(object ct, object source, string nodeID)
 {
     if (ct == null)
     {
         return;
     }
     if (ct is XHtmlAction)
     {
         #region XHtmlAction 对象处理
         XHtmlAction doc = ct as XHtmlAction;
         MDataTable  dt  = source as MDataTable;
         doc.LoadData(dt);
         XmlNode node = null;
         if (string.IsNullOrEmpty(nodeID))
         {
             doc.SetForeach();
         }
         else
         {
             node = doc.Get(nodeID);
             if (node != null)
             {
                 doc.SetForeach(node, node.InnerXml);
             }
         }
         #endregion
     }
     else
     {
         #region 检测下拉列表控件
         if (ct is ListControl)
         {
             BindList(ct as ListControl, source as MDataTable);
         }
         else if (ct is Win.ListControl)
         {
             BindList(ct as Win.ListControl, source as MDataTable);
         }
         else
         {
             Type         t = ct.GetType();
             PropertyInfo p = t.GetProperty("DataSource");
             if (p != null)
             {
                 #region DataGridView处理
                 MethodInfo meth = t.GetMethod("DataBind");
                 if (meth != null)//web
                 {
                     p.SetValue(ct, source, null);
                     meth.Invoke(ct, null);
                 }
                 else
                 {
                     if (source is MDataTable)
                     {
                         MDataTable dt = source as MDataTable;
                         source = new MDataView(ref dt);
                     }
                     p.SetValue(ct, source, null);//winform
                 }
                 #endregion
             }
             else //wpf,sliverlight
             {
                 p = t.GetProperty("ItemsSource");
                 if (p != null)
                 {
                     MDataTable dt = null;
                     if (source is MDataTable)
                     {
                         dt     = source as MDataTable;
                         source = dt.ToDataTable().DefaultView;
                     }
                     p.SetValue(ct, source, null);           //winform
                     p = t.GetProperty("SelectedValuePath"); //判断是不是下拉列表
                     if (p != null)
                     {
                         p.SetValue(ct, dt.Columns[0].ColumnName, null);
                         p = t.GetProperty("DisplayMemberPath");
                         p.SetValue(ct, dt.Columns[dt.Columns.Count > 1 ? 1 : 0].ColumnName, null);
                     }
                 }
             }
         }
         #endregion
     }
 }
Exemple #2
0
        public override void GetData(object target, System.IO.Stream outgoingData)
        {
            MDataTable dt = null;
            #region 类型判断

           
            if (target is MDataTable)
            {
                dt = target as MDataTable;
            }
            else if (target is MDataRow)
            {
                dt = ((MDataRow)target).ToTable();
            }
            else if (target is MDataColumn)
            {
                dt = ((MDataColumn)target).ToTable();
            }
            else if (target is MDataRowCollection)
            {
                dt = target as MDataRowCollection;
            }
            else if (target is DataRow)
            {
                MDataRow row = target as DataRow;
                dt = row.ToTable();
            }
            else if (target is DataColumnCollection)
            {
                MDataColumn mdc = target as DataColumnCollection;
                dt = mdc.ToTable();
            }
            else if (target is DataRowCollection)
            {
                MDataRowCollection rows = target as DataRowCollection;
                dt = rows;
            }
            else if (target is NameObjectCollectionBase)
            {
                dt = MDataTable.CreateFrom(target as NameObjectCollectionBase);
            }
            else if (target is IEnumerable)
            {
                dt = MDataTable.CreateFrom(target as IEnumerable);
            }
            else
            {
                dt = MDataTable.CreateFrom(target);
                if (dt == null)
                {
                    MDataRow row = MDataRow.CreateFrom(target);
                    if (row != null)
                    {
                        dt = row.ToTable();
                    }
                }
            }
            #endregion
            dt = Format(dt);
            if (dt != null)
            {
                base.GetData(dt.ToDataTable(), outgoingData);
            }
            else
            {

                base.GetData(new DataTable("Empty Table"), outgoingData);
            }
        }