如何在c#中显示树视图中id及其子id的数据 [英] How to display a data of id and its sub ids in treeview in c#

查看:58
本文介绍了如何在c#中显示树视图中id及其子id的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在树视图中显示id及其子ID的数据

这是我在c#和ms访问数据库中的代码,它不显示所有内容



hOW TO display a data of id and its sub ids in treeview
this is my code in c# and ms access data base it not displaying all of it

private void button2_Click(object sender, EventArgs e)
{
    l:

        try
        {

            con = new OleDbConnection(cs);
            con.Open();
            //Sales.ReferenceID = Customer.CustomerID and
            cmd = new OleDbCommand("SELECT * from Sales where   CustomerID='" + textBox22.Text + "' order by  CustomerID ASC ", con);
            OleDbDataAdapter myDA = new OleDbDataAdapter(cmd);
            DataTable rdr = new DataTable();
            myDA.Fill(rdr);

            for (int i = 0; i < rdr.Rows.Count; i++)
            {
                TreeNode node = new TreeNode(rdr.Rows[i]["CustomerNameSales"].ToString());
                node.Nodes.Add(rdr.Rows[i]["ReferenceID"].ToString());
                node.Nodes.Add(rdr.Rows[i]["CustomerID"].ToString());
                node.Nodes.Add(rdr.Rows[i]["Rank"].ToString());
                node.Nodes.Add(rdr.Rows[i]["checkcol"].ToString());
                treeView1.Nodes.Add(node);
            }


            con.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        try
        {

            con = new OleDbConnection(cs);
            con.Open();
            String ct102000 = "SELECT CustomerID from Sales where ReferenceID = '" + textBox22.Text + "'";
            cmd = new OleDbCommand(ct102000, con);
            rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);


            if ((rdr.Read() == true))
            {

                textBox21.Text = (rdr["CustomerID"].ToString());
                textBox22.Text = textBox21.Text;

                if (textBox22.Text != null)
                {
                    goto l;

                }

                goto l;
            }

            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }



}



给我一个解决方案

添加了代码块 - OriginalGriff [/ edit]


give me a solution
[edit]Code block added - OriginalGriff[/edit]

推荐答案

在正常情况下,我会查看该代码并尝试找出问题所在。

但是在这种情况下,我不会这样做,因为代码显然编写得很糟糕,修复它的问题 - 你所看到的问题只是其中最小的问题。



No.

把那个地方丢进垃圾箱里。坐下来思考你想要做什么,然后从头开始。而这一次,忘记关键字 goto 曾经存在过。不要再写这样的代码:

Under normal circumstances, I would look at that code and try to work out what is wrong.
But in this case, I'm not going to, because the code is clearly so poorly written, and fixing it's problem-as-seen-by-you is just the least of it's problems.

No.
Throw that lot in the bin. Sit down and think about what you are trying to do and start again from scratch. And this time, forget that the keyword goto ever existed. Do not write code like this again:
if ((rdr.Read() == true))
{

    textBox21.Text = (rdr["CustomerID"].ToString());
    textBox22.Text = textBox21.Text;

    if (textBox22.Text != null)
    {
        goto l;

    }

    goto l;
}

永远。事实上,不要使用标签,或 goto 至少五年。到那时,你应该明白它何时是合适的......

Ever. In fact, do not use a label, or goto for at least five years. By then, you should understand when it is and isn't appropriate...


这篇关于如何在c#中显示树视图中id及其子id的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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