今天开发过程中遇到Combobox绑定数据的需求,研究了下绑定DataTable,现在Po上来,有时间研究下绑定其他类型的数据,到时候再来更新。
废话不多说,上代码:
//清空一下绑定 m_ComboBoxServerName.DataSource = null; //构造待绑定的数据表 DataTable dataTable = new DataTable(); //构造数据表结构 dataTable.Columns.Add("SeverName",typeof(String)); dataTable.Columns.Add("SeverAddress", typeof(String)); dataTable.Columns.Add("SeverType", typeof(String)); //数据表村值 dataTable.Rows.Add(new String[] { "全球矢量地图(经纬度)", "http://t0.tianditu.com/vec_c/wmts", "WMTS" }); dataTable.Rows.Add(new String[] { "全球矢量地图(墨卡托)", "http://t0.tianditu.com/vec_w/wmts", "WMTS" }); dataTable.Rows.Add(new String[] { "全球矢量中文注记服务(经纬度)", "http://t0.tianditu.com/cva_c/wmts", "WMTS" }); dataTable.Rows.Add(new String[] { "全球矢量中文注记服务(墨卡托)", "http://t0.tianditu.com/cva_w/wmts", "WMTS" }); dataTable.Rows.Add(new String[] { "全球影像地图服务(经纬度)", "http://t0.tianditu.com/img_c/wmts", "WMTS" }); dataTable.Rows.Add(new String[] { "全球影像地图服务(墨卡托)", "http://t0.tianditu.com/img_w/wmts", "WMTS" }); dataTable.Rows.Add(new String[] { "全球地形晕渲地图服务(经纬度)", "http://t0.tianditu.com/ter_c/wmts", "WMTS" }); //绑定数据表 m_ComboBoxServerName.DataSource = dataTable; //指定显示值 m_ComboBoxServerName.DisplayMember = dataTable.Columns[0].ColumnName; //指定绑定值 m_ComboBoxServerName.ValueMember = dataTable.Columns[1].ColumnName; private void m_ComboBoxServerName_SelectedIndexChanged(object sender, EventArgs e)
{ try { //获取绑定值 String serverAddress = m_ComboBoxServerName.SelectedValue.ToString(); } catch (Exception ex) { Application.ActiveApplication.Output.Output(ex); }
}