使用值下载正确的数据并更新 [英] Download Correct data with values and update

查看:77
本文介绍了使用值下载正确的数据并更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4张桌子:

i got 4 tables:

plain_table(plain_id(PK), plain_name, FK_temperature_id(FK), FK_material_id(FK), FK_drive_id(FK))

temperature(temperature_id(PK), temperature_name)

material(material_id(PK), material_name)

drive(drive_id(PK), drive_name)



我将名称下载到列表框的代码:




A code i download names to listbox:


listBox1.Items.Clear();
conn.Open();
string query = ("SELECT plain_name FROM plain_table");
SqlCommand command = new SqlCommand(query, conn);
command.ExecuteNonQuery();

SqlDataReader dr = command.ExecuteReader(Commandbehavior.CloseConnection);

while (dr.Read())
{
    string plain_name = Convert.ToString(dr["plain_name"]);
    this.listBox1.Items.Add(plain_name);
}

conn.Close();



我在列表框上得到了一个名字...现在,我想单击其中的一部分并将记录的值下载到控件:



I got a name''s on listbox... Now i would love to click on some of it and download values of record to controls:

plain_name to textBox control

FK_temperature_id(FK) to comboBox control

FK_material_id(FK) to comboBox control FK_drive_id(FK)) to comboBox control 



如此鼠标点击事件

我试图做到这一点,但我做到了,但是我在组合框中有一个整数值,因此当有人要更改此fk时,它不是用户友好的-应该是名称而不是值,但是如果我这样做,我不知道如何发送更新案例,现在它是varchar.我希望你明白我的意思.我认为数据集,值成员和显示成员应该有所帮助,但我不知道如何.


得到此代码以获取带有名称的值到列表框的值,但是如何进行更新?如果现在在fk的组合框中显示varchar?其次,我希望所有名称都显示在该fk的组合框中,但实际上设置为关联.



so on mouse click event

i tried to do it and i done but i have in comboboxes an integer values so it''s not user friendly when someone want to change this fk - should be a name not value, but if i do it i don''t know how to send update case n ow it''s varchar. I hope u understand what i mean. I think a dataset and value member and display member should help but i don''t know how.


Got this code for take a values to listbox with names, but how to make an update? If in fk''s comboboxes are now display a varchar? Second is i would love to have all names displayed on that fk''s comboboxes but set on actually is relation.

private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            PobranieWartosciDoKontrolek();

        }



        private void PobranieWartosciDoKontrolek()
        {
            plain_name = listBox1.Items[listBox1.SelectedIndex].ToString();
            conn.Open();
            string zapytanie = ("SELECT plain_id, plain_name, FK_temperature_id, FK_material_id, FK_drive_id, temperature.temperature_name, material.material_name, drive.drive_name FROM plain_table LEFT JOIN temperature ON temperature.temperature_id = plain_table.FK_temperature_id LEFT JOIN material ON material.material_id = plain_table.FK_material_id LEFT JOIN drive ON drive.drive_id = plaintable.FK_drive_id WHERE plain_name=''" + plain_name + "'';");
            SqlCommand command = new SqlCommand(zapytanie, conn);
            command.ExecuteNonQuery();

            SqlDataReader dr = command.ExecuteReader(Commandbehavior.CloseConnection);
            dr.Read();

            textBox1.Text = Convert.ToString(dr["plain_name"]);
            comboBox1.Text = Convert.ToString(dr["temperature_name"]);
            comboBox2.Text = Convert.ToString(dr["material_name"]);
            comboBox3.Text = Convert.ToString(dr["drive_name"]);

            conn.Close();
        }

推荐答案

最简单的方法是定义自己的类,并重写ToString方法.这样,ToString的输出将显示在您的列表框中,但是Items集合中的对象包含您需要的所有信息:
The easiest way is to define your own class, and override the ToString method. That way, the output from the ToString is displayed inyou listbox, but the object in the Items collection contains all the information you need:
private void MyForm_Load(object sender, EventArgs e)
    {
    int i = 100;
    string[] words = "The quick Brown fox jumps over the lazy dog".Split(' ');
    foreach (string s in words)
        {
        myListBox.Items.Add(new MyItem(i++, s));
        }
    }
private class MyItem
    {
    public int Index { get; set; }
    public string Value { get; set; }
    public MyItem(int i, string s)
        {
        Index = i;
        Value = s;
        }
    public override string ToString()
        {
        return Value;
        }
    }
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
    ListBox lb = sender as ListBox;
    if (lb != null)
        {
        int index = lb.SelectedIndex;
        MyItem mi = lb.Items[index] as MyItem;
        Console.WriteLine(mi + ":" + mi.Index.ToString());
        }
    }

如果将数据库外键值与MyItem中的字符串一起保留,则可以根据需要直接在SelectedIndexChanged事件中使用该值来填充其他列表框.

If you keep the database Foreign Key value with the string in the MyItem, you can use that directly in the SelectedIndexChanged event to populate the other listbox(s) as needed.


这篇关于使用值下载正确的数据并更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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