Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi I am trying to store value of combo box selected Item in variable.
but it doesn't work properly.

according to my code when I press edit button it fills combo box.

and when index value changes it should show the string which I selected in combobox.

but its shows something like
system.data.datarowview

What I have tried:

private void btnedit_Click(object sender, EventArgs e)
{

cmdpr.Enabled = true;
SqlConnection connection = new SqlConnection(cn);
SqlCommand cmd1 = new SqlCommand("select ProjectName,ProjectID from Project", connection);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
connection.Open();
DataSet ds = new DataSet();
da.Fill(ds, "PN");
cmdpr.DisplayMember = "ProjectName";
cmdpr.ValueMember = "ProjectID";
cmdpr.DataSource = ds.Tables["PN"];
connection.Close();
}

private void cmdpr_SelectedIndexChanged(object sender, EventArgs e)
{
string s = cmdpr.SelectedItem.ToString();
MessageBox.Show(s);

}
Posted
Updated 12-Sep-16 16:34pm

1 solution

Try casting the cmdpr.SelectedItem to a DataRowView object and select the column you want to work with by name like the following:

C#
MessageBox.Show((cmdpr.SelectedItem as DataRowView).Row["ProjectName"].ToString())
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900