c# 中绑定combox,同时获取values和text值

public void BindCombox(ComboBox cb, string id, string text,string str) //id valuetexttextstr为得到数据集的语句

        {

            DataTable dt = new DataTable();

            DataSet ds = new DataSet();

            ds = GetDataSetFromSqlString(str);

            if (ds.Tables.Count > 0)

            {

                dt = GetDataSetFromSqlString(str).Tables[0];

                DataRow dr = dt.NewRow();

                dr[0] = "-100";

                dr[1] = "请选择";

                //插在第一位 

                dt.Rows.InsertAt(dr, 0);

                cb.Items.Clear();

                cb.DataSource = dt;

                cb.DisplayMember = text;

                cb.ValueMember = id;

            }

        }

//通过combox.selectIndexchanged方法,用string x=combox1.selectValues.tostring()得到values值

你可能感兴趣的:(c# 中绑定combox,同时获取values和text值)