简体   繁体   中英

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

Using selectedIndexChange for Combobox to fill Textboxes in Access Database.

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. If SelectedValue is Matching to MyData Row Index then set event Date.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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