简体   繁体   中英

SelectedValue then get data from database

ConfigurationDAL dAL = new ConfigurationDAL();
DataTable dt = new DataTable();
            
if(drp_Volunteeer.SelectedValue !=null)
{
    dt = dAL.FetchVolunteerDetails(volunteerID: drp_Volunteeer.SelectedIndex).Tables[0];             

    foreach (DataRow dr in dt.Rows)
    {
        txt_FirstName.Text = dr["firstname"].ToString();
        txt_fatherName.Text = dr["fathername"].ToString();
    }
}

SelectedValue count shows 32:

SelectedValue 计数显示 32

SelectedIndex works fine:

Selectedindex 工作正常

Filling the TextBox is not working.

User Not Selected

User Selected

When I User Select Data Can't Revtive Data From DataBase:

I don't know how you can fill (add) data to SelectedBox . But you maybe know, SelectedBox's Items supports any object, you can add any objects to items. I usually create my custom class for filling SelectedBoxs. For Example, you can create Volunteeer class for adding, and viewing data on the SelectedBox. Example:

public class Volunteeer
{
    private int id;
    private string firstname;
    private string lastname;

    public Volunteeer(int pid, string fname, string lname)
    {
        id = pid;
        firstname = fname;
        lastname = lname;
    }

    public string GetFirstName()
    {
        return firstname;
    }

    public string GetLastName()
    {
        return lastname;
    }

    public int GetId()
    {
        return id;
    }

    public override string ToString()
    {
        return firstname + ' ' + lastname;
    }

}

Method ToString() are for viewing text data format on the SelectedBox. After then you can very easily get selected items. Example:

private void SelectedText_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (SelectedText.SelectedIndex >= 0)
        {
            txtFirstName.Text = (SelectedText.SelectedValue as Volunteeer).GetFirstName();
            txtLastName.Text = (SelectedText.SelectedValue as Volunteeer).GetLastName();
        }
    }

And this is a sample how you can fill data to SelectedBox:

SelectedText.Items.Add(new Volunteeer(1, "Sara", "Montano"));
SelectedText.Items.Add(new Volunteeer(2, "Jon", "Smith"));
SelectedText.Items.Add(new Volunteeer(3, "Tom", "Serato"));

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