c# winform 程序 datagridview 绑定了一个数据库的表,怎么加入DataGridViewComboB
c# winform 程序 datagridview 绑定了一个数据库的A表,怎么加入DataGridViewComboB?DataGridViewComboB 的数据源是另一个B表,查了很多资料,都出错。
方法1:
namespace Sys
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoGenerateColumns = false;
using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=sa;database=xx"))
{
string sql1 = @"select userid,fullname,a.[description],b.ugroupid
from users a left outer join usergroup b
on a.grade=b.ugroupid";
string sql2 = "select distinct ugroupid,[description] from usergroup";
DataGridViewComboBoxColumn dgvComboBoxColumn = dataGridView1.Columns["grade"] as DataGridViewComboBoxColumn;
dgvComboBoxColumn.DataPropertyName = "ugroupid";
dgvComboBoxColumn.DataSource = GetTable(sql2).DefaultView;//必须在设置dataGridView1的DataSource的属性前设置
dgvComboBoxColumn.DisplayMember = "description";
dgvComboBoxColumn.ValueMember = "ugroupid";
dataGridView1.DataSource = GetTable(sql1).DefaultView;//一定要在dgvComboBoxColumn的DataSource后设置
}
}
private DataTable GetTable(string sql)
{
using (SqlConnection sqlconn = new SqlConnection("server=.;uid=sa;pwd=yuling1310;database=smls"))
{
DataTable dt = new DataTable();
SqlDataAdapter sqlda = new SqlDataAdapter(sql, sqlconn);
sqlda.Fill(dt);
return dt;
}
}
}
}
方法2:
NetFramework 2.0里提供的DataGridViewComboBoxColumn只有在以下情况下才正常工作:它的所有单元格值(由 DataGridView.DataSource属性填充)与选择的范围(由 DataSource属性或 Items属性填充)之间存在映射。如果此映射不存在,则当该列位于视图中时,会出现异常“DataGridViewComboBoxCell 值无效”。 1, #region 枚举名和值转换到DataTable
/// <summary>/// 枚举名和值转换到DataTable/// </summary>/// <param name="enumType">枚举实例</param>/// <param name="key">名</param>/// <param name="val">值</param>/// <returns>DataTable</returns>publicstatic DataTable Enum2DataTable(Type enumType, string key, string val)
{
// 获取所有枚举的名称string[] names = Enum.GetNames(enumType);// 获取所有枚举的值
Array values = Enum.GetValues(enumType);
DataTable dict = new DataTable();
dict.Columns.Add(new DataColumn(key));
dict.Columns.Add(new DataColumn(val));
dict.Columns[val].Unique = true;
dict.Columns[val].AllowDBNull = false;//如果这里不指定和DataGrid对应列相同的列类型就会抛出DataError异常
dict.Columns[val].DataType = Type.GetType("System.Int32");for (int i = 0; i < values.Length; i++)
{
DataRow dr = dict.NewRow();
dr[key] = names[i];
dr[val] = (int)values.GetValue(i);
dict.Rows.Add(dr);
}
return dict;
}
#endregion 2,
privatevoid btnAllSite_Click(object sender, EventArgs e)
{
DataGridViewComboBoxColumn cbcSiteStatus = (DataGridViewComboBoxColumn)dgvSiteList.Columns["colStatus"];
DataTable cbdSource = Commen.Enum2DataTable(typeof(SiteStatus), cbcSiteStatus.HeaderText, cbcSiteStatus.DataPropertyName);
cbcSiteStatus.DataSource = cbdSource;
cbcSiteStatus.DisplayMember = cbcSiteStatus.HeaderText;
cbcSiteStatus.ValueMember = cbcSiteStatus.DataPropertyName;
cbcSiteStatus.DataPropertyName = "status";
DataTable dt = KobSky.SiteGuide.CoreLib.DBfunc.WebSite.AllSite();
dgvSiteList.DataSource = dt;
}
方法1,尝试了很多次,都失败。
方法2,映射不会。希望有高人,教我怎么映射。
“它的所有单元格值(由 DataGridView.DataSource属性填充)与选择的范围(由 DataSource属性或 Items属性填充)之间存在映射。。。”
这具体怎么做?
补充:.NET技术 , C#