如何在datagridview中表示数据,就像wpf应用程序中的树视图一样 [英] How to data represent in datagridview like a tree view in wpf application

查看:75
本文介绍了如何在datagridview中表示数据,就像wpf应用程序中的树视图一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮帮我

私人ObservableCollection< treemodel> tempAddData()

{

ObservableCollection< treemodel> temp = new ObservableCollection< treemodel>();

// new treeModel {IsExpand = false,Name =megh,Contact =122,Address =ktm,IsVisible =Visibile ,ParentName =},

// new treeModel {IsExpand = false,Name =sita,Address =pk,Contact =123456,IsVisible =Visibile,ParentName = megh},

// new treeModel {IsExpand = false,Name =gita,Address =lon,Contact =988484,IsVisible =Collapsed,ParentName =sita },

// new treeModel {IsExpand = false,Name =ranjit,Address =india,Contact =984,IsVisible =Collapsed,ParentName =sita} ,

// new treeModel {IsExpand = false,Name =teatime,Address =kkf,Contact =2144,IsVisible =Visible,ParentName =megh},

// new treeModel {IsExpand = false,Name =execuew,Address =6644,Contact =1233,IsVisible =Visibile,ParentName =teatime},
// new treeModel {IsExpand = false, Name =ee,Address =1233,Contact =122,IsVisible =Collapsed,ParentName =execuew} //使用工作

(SqlConnection cmain = new SqlConnection(GlobleClass) .DataConnectionString))

使用(SqlCommand cmd = cmain.CreateCommand())

{

cmain.Open();

cmd.CommandText = @SELECT * FROM MENUITEM WHERE LEVELS = 1 and parent ='MI'; //不工作





使用(SqlDataReader dr = cmd.ExecuteReader())

{

while(dr.Read())

{

temp.Add(new treeModel

{



DESCA = dr [DESCA]。ToString() ,

Parent = dr [Parent]。ToString(),

MGROUP = dr [MGROUP]。ToString(),

MCAT = dr [MCAT]。ToString()





});

}



}



}



返回临时;

}

Please help me
private ObservableCollection<treemodel> tempAddData()
{
ObservableCollection<treemodel> temp = new ObservableCollection<treemodel>();
//new treeModel { IsExpand=false, Name = "megh",Contact="122" ,Address="ktm" ,IsVisible="Visibile", ParentName="" },
//new treeModel { IsExpand=false , Name = "sita", Address="pk",Contact="123456" , IsVisible="Visibile" , ParentName="megh" },
//new treeModel { IsExpand=false, Name = "gita",Address="lon" ,Contact="988484", IsVisible="Collapsed" , ParentName="sita"},
//new treeModel { IsExpand=false, Name = "ranjit",Address="india",Contact="984" , IsVisible="Collapsed" , ParentName="sita"},
//new treeModel { IsExpand=false, Name = "teatime",Address="kkf" ,Contact="2144", IsVisible="Visible" , ParentName="megh"},
//new treeModel { IsExpand=false, Name = "execuew",Address="6644" ,Contact="1233" ,IsVisible="Visibile" , ParentName="teatime"},
//new treeModel { IsExpand=false, Name = "ee", Address="1233" ,Contact="122", IsVisible="Collapsed", ParentName="execuew"} //Working
using (SqlConnection cmain = new SqlConnection(GlobleClass.DataConnectionString))
using (SqlCommand cmd = cmain.CreateCommand())
{
cmain.Open();
cmd.CommandText = @"SELECT * FROM MENUITEM WHERE LEVELS =1 and parent='MI'";//Not working


using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
temp.Add(new treeModel
{

DESCA = dr["DESCA"].ToString(),
Parent = dr["Parent"].ToString(),
MGROUP = dr["MGROUP"].ToString(),
MCAT = dr["MCAT"].ToString()


});
}

}

}

return temp;
}

推荐答案

使用下面的代码,

Use the following code,
private ObservableCollection tempAddData()
{
    ObservableCollection temp = new ObservableCollection();
    DataTable objTable = new DataTable();

    SqlConnection objConnection = new SqlConnection("Connection String Here");
    objConnection.Open();
    SqlCommand objSqlCommand = new SqlCommand("Query Here", objConnection);
    SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objSqlCommand);
    objSqlDataAdapter.Fill(objTable);
    objConnection.Close();

    foreach(DataRow objRow in objTable.Rows)
    {
        treeModel objtreeModel =  new treeModel();

        objtreeModel.DESCA = objRow["DESCA"].ToString();
        objtreeModel.Parent = objRow["Parent"].ToString();
        objtreeModel.MGROUP = objRow["MGROUP"].ToString();
        objtreeModel.MCAT = objRow["MCAT"].ToString();

        temp.Add(objtreeModel);
    }

    return temp;
}


这篇关于如何在datagridview中表示数据,就像wpf应用程序中的树视图一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆