如何从来自sqlreader的组合框中获取selecteditem中的其他值?

时间:2016-03-31 13:35:38

标签: c# sql

我真的不知道如何在标题中提出具体问题但是。如何从comboBox selectedItem获取Lname,Fname和Mname字符串。这是我的想法,但它不起作用:/

from matplotlib import pyplot as plt

class LineBuilder:
    def __init__(self, line):
        self.line = line
        self.xs = list(line.get_xdata())
        self.ys = list(line.get_ydata())
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        print('click', vars(event))
        if event.button==3:
            print('clean up')
            event.canvas.mpl_disconnect(self.cid)
            return
        if event.inaxes != self.line.axes:
            return
        self.xs.append(event.xdata)
        self.ys.append(event.ydata)
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw_idle()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0])  # empty line
linebuilder = LineBuilder(line)

plt.show()

1 个答案:

答案 0 :(得分:0)

我要做的是用文本和值对象填充组合框,这样就可以将名称存储在值中并轻松检索它们:

private void FillWinemakerComboBox()
{
    SqlCommand cmand = new SqlCommand("SELECT (Lname+', '+Fname+' '+Mname) AS combinedName, Lname, Fname, Mname FROM Winemaker", con);
    con.Open();
    SqlDataReader sqlReader = cmand.ExecuteReader();
    Dictionary<string, string> comboSource = new Dictionary<string, string>();

    while (sqlReader.Read())
    {
        var key = String.Format("{0},{1},{2}", sqlReader["Lname"].ToString(), sqlReader["Fname"].ToString(), sqlReader["Mname"].ToString());
        comboSource.Add(key, sqlReader["combinedName"].ToString());
    }

    winemaker_comboBox.DataSource = new BindingSource(comboSource, null);
    winemaker_comboBox.DisplayMember = "Value";
    winemaker_comboBox.ValueMember = "Key";
    sqlReader.Close();
    con.Close();
}

你像这样填充你的组合框,然后检索这样的项目:

if (winemaker_comboBox.SelectedItem != DBnull.Value)
{
    var names = winemaker_comboBox.SelectedValue.ToString().Split(',');
    string lastname = names[0];
    string firstname = names[1];
    string middlename = names.Length > 2 ? names[2] : string.Empty;
}