如何识别C#中dataTreeListView1中节点的按钮单击

时间:2017-07-23 04:22:24

标签: c# winforms objectlistview

我有dataTreeListView名为dataTreeListView1我希望得到我点击的节点的名称可以任何一个请帮我做这个

enter image description here

在上面的图片中,如果我点击是,我希望在messagebox上获得

我试图做的是

private void dataTreeListView1_AfterSelect(object sender, EventArgs e)
        {
            MessageBox.Show("hello");
        }

并且动态加载节点意味着它们来自数据库。

List<Class3> list = new List<Class3>();
                    list = Class3.GetList(startDate, endDate, con);
                    this.dataTreeListView1.ParentKeyAspectName = "ParentId";
                    this.dataTreeListView1.RootKeyValueString = "NUll";
                    BrightIdeasSoftware.OLVColumn col = new BrightIdeasSoftware.OLVColumn();
                    col.Width = 10;
                    dataTreeListView1.DataSource = list;
                    foreach (ColumnHeader column in this.dataTreeListView1.Columns)
                    {
                        column.Width = 140 + 140 + this.dataTreeListView1.SmallImageSize.Width;
                    }
                    this.dataTreeListView1.Columns.RemoveByKey("Id");
                    this.dataTreeListView1.Columns.RemoveByKey("ParentId");

这就是我在datatreelist视图中显示数据的方式

 public Class3()
        {
            this.xName = "";
            this.xId = "";
            this.xParentId = "";
            this.xcredit = 0.0M;
            this.xdebit = 0.0M;
            this.xx = 0.0M;
            this.xy = 0.0M;
        }

        public String Ledger
        {
            get { return this.xName; }
            set { this.xName = value; }
        }

        public String Id
        {
            get { return this.xId; }
            set { this.xId = value; }
        }

        public String ParentId
        {
            get { return this.xParentId; }
            set { this.xParentId = value; }
        }

        public Decimal Credit
        {
            get { return this.xcredit; }
            set { this.xcredit = value; }
        }

        public Decimal Debit
        {
            get { return this.xdebit; }
            set { this.xdebit = value; }
        }
        public decimal x
        {
            get { return this.xx; }
            set { this.xx = value; }
        }
        public decimal y
        {
            get { return this.xy; }
            set { this.xy = value; }
        }
        public static List<Class3> GetList(DateTime startDate, DateTime endDate, SqlConnection con)
        {
            List<Class3> oList = new List<Class3>();
            Buisiness b = new Buisiness();
            try
            {
                decimal totalcredit = 0;
                decimal totaldebit = 0;
                con.Open();
                //    // Make sql readable
                DataTable dt = new DataTable();
                string sql = @"Select Ledger.LedId,Ledger.LedName,Ledger.MasterLedger from Ledger where Ledger.Date >= @prmStartDate and Ledger.Date <= @prmEndDate group by Ledger.MasterLedger,Ledger.LedId,Ledger.LedName";

                // wrap IDisposable (SqlCommand) into using
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    cmd.Parameters.Add("@prmStartDate", SqlDbType.DateTime).Value = startDate;
                    cmd.Parameters.Add("@prmEndDate", SqlDbType.DateTime).Value = endDate;
                    SqlDataReader da = cmd.ExecuteReader();
                    while (da.Read())
                    {
                        Class3 oClass = new Class3();
                        oClass.Ledger = da["LedName"].ToString();
                        oClass.Id = da["LedId"].ToString();
                        oClass.ParentId = da["MasterLedger"].ToString();
                        Dictionary<string, decimal> d = b.findclosingbalanceofledger(da["LedId"].ToString());
    /*                    if (da["MasterLedger"].ToString() == "NUll")
                      {
                            oClass.Debit = findsumofallchilds(da["LedId"].ToString(), con, startDate, endDate);
                        }
                       else
                       {*/
                            if (d.FirstOrDefault().Key == "debit")
                            {
                                d.FirstOrDefault().Value.ToString();
                                d.FirstOrDefault().Value.ToString();
                                totaldebit = totaldebit + d.FirstOrDefault().Value;
                                oClass.Debit = d.FirstOrDefault().Value;
                            }
                            else if (d.FirstOrDefault().Key == "credit")
                            {
                                d.FirstOrDefault().Value.ToString();
                                totalcredit = totalcredit + d.FirstOrDefault().Value;
                                oClass.Credit = d.FirstOrDefault().Value;
                            }
  /*                      }*/
                        oList.Add(oClass);


                    }
                }
                con.Close();
            }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message);
            }
            finally
            {
                con.Close();
            }
            return oList;
        }

我已经改变了我的代码

 private void dataTreeListView1_AfterSelect(object sender, EventArgs e)
            {
                MessageBox.Show("hello");
            }

private void dataTreeListView1_CellClick(object sender, CellClickEventArgs e)
{
    MessageBox.Show("helo");
}

但没有变化

0 个答案:

没有答案