简体   繁体   English

在C#中,如何使用Combobox和Textbox选择索引

[英]In C#, How to use Selected Index with Combobox and Textbox

Using selectedIndexChange for Combobox to fill Textboxes in Access Database. 使用selectedIndexChange for Combobox填充Access数据库中的文本框。

I've tried using the following: 我尝试过使用以下内容:

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

but it doesn't fill in from the selected data. 但它不会从所选数据中填写。

// 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()));
}

Try like this to set EventDate for Selected Index value. 尝试这样设置EventDate for Selected Index值。 If SelectedValue is Matching to MyData Row Index then set event Date. 如果SelectedValue与MyData Row Index匹配,则设置事件日期。

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();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM