在C#中,如何通过组合框和文本框使用选定的索引

时间:2019-04-20 22:09:18

标签: c# sql database combobox textbox

使用Combobox的selectedIndexChange来填充Access数据库中的文本框。

我尝试使用以下内容:

txtEventDate.Text = cboEventName.SelectedValue.ToString();

但它不是根据所选数据填写的。

// clear out listbox
cboEventName.Items.Clear();

// create instance of class
clsData myData = new clsData();

// send SQL statement to class
myData.SQL = "SELECT ID, EventName, EventDate FROM tblEvents ORDER BY EventName";

// loop through datatable to get values
for (int i = 0; i < myData.dt.Rows.Count; i++)
{
    // add customer to list box
    cboEventName.Items.Add(myData.dt.Rows[i]["EventName"].ToString());
    // add customer id to list
    string eventdate = ["EventDate"].ToString();
    txtEventDate.Text = cboEventName.SelectedValue.ToString();
    // txtEventDate trying to fill from combobox entry and it isn't showing the date from the access.
    intEventID.Add(int.Parse(myData.dt.Rows[i]["ID"].ToString()));
}

1 个答案:

答案 0 :(得分:0)

尝试像这样为选定索引值设置EventDate。如果SelectedValue与MyData行索引匹配,则设置事件日期。

for (int i = 0; i < myData.dt.Rows.Count; i++)
            {
                // add customer to list box
                cboEventName.Items.Add(myData.dt.Rows[i]["EventName"].ToString());
                // add customer id to list
                if(cboEventName.SelectedValue.ToString() ==myData.dt.Rows[i]["EventName"].ToString())
                    string eventdate = ["EventDate"].ToString();
                txtEventDate.Text = cboEventName.SelectedValue.ToString();
            }