关于datagridview和dataset

music-4580 2017-08-28 08:49:54
把数据库中的数据填充到dataset后,把datagridview的数据源绑定到dataset。现在有一个问题是,datagridview中有一个字段需要设置为combox下拉列表的形式,这个实现是可以实现。问题是数据库中的字段也会出现在datagridview中,这样就产生了2个同样的字段,一个是普通的 一个是combox的。不知道怎么是好了 求大神指教!
...全文
181 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
易2017 2017-08-28
  • 打赏
  • 举报
回复
禁止datagridview的自动添加列,在设计器中添加项,绑定你要显示的字段,高效亲测
一品梅 2017-08-28
  • 打赏
  • 举报
回复
DataGridViewComboBoxColumn DataSource? 问题: I'm trying to get something set up in a DataGridView. It seems like this should be pretty straightforward but I'm having trouble. I want to display three columns: CodeID CodeName ComboBox with DisplayMember of TypeName, ValueMember of TypeID I want to be able to select from all possible values of TypeName. Here's my dilemma: If I load all of this into one DataTable and set the DataGridView as the DataSource, I can display the existing TypeName for that record, but the combo box will not include any other values. If I set the DataSource for the DataGridViewComboBoxColumn to a separate DataTable that includes all possible TypeNames, the existing value is not displayed. DataGridView is really annoying to work with so either a solution for this or a viable alternative would be appreciated. Edit: it appears the issue is caused by my wanting to have a separate item for DisplayMember and ValueMember. The following works, if I don't worry about setting the ID as the ValueMember:
var typeColumn = new DataGridViewComboBoxColumn{
    DataSource = typeList,
    DisplayMember = "Type",
    ValueMember = "Type",
    DataPropertyName = "Type"}
If I do the following, the right types are selected, but I can't change the selection in the combo box:
var typeColumn = new DataGridViewComboBoxColumn{
    DataSource = typeList,
    DisplayMember = "Type",
    ValueMember = "TypeID",
    DataPropertyName = "TypeID"}
If I use the following I get a FormatException error as it's trying to populate:
var typeColumn = new DataGridViewComboBoxColumn{
    DataSource = typeList,
    DisplayMember = "Type",
    ValueMember = "TypeID",
    DataPropertyName = "Type"}
edit: typeList is a simple DataTable populated by the following: SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]FROM InsuranceType ITWHERE IT.ClientID = @ClientIDORDER BY [Type] 回答: Ok, I came up with an example ClientInfo and uranceDetails that I think might mimic what you are trying to do. Let me know if these details arent quite right. This example will populate the DataGridViewComboBox and set the value based on the InsuranceDetails specifically at: InsurDetailz = all_insurance_types[2])
 public partial class Form1 : Form
   {
      private ClientInfo _myClient;
      private BindingList<InsuranceDetails> all_insurance_types =
         new BindingList<InsuranceDetails>();

      public Form1()
      {
         InitializeComponent();

         DataGridView grid = new DataGridView();
         grid.Dock = DockStyle.Fill;
         grid.AutoGenerateColumns = true;

         all_insurance_types.Add(new InsuranceDetails(1, "Health"));
         all_insurance_types.Add(new InsuranceDetails(2, "Home"));
         all_insurance_types.Add(new InsuranceDetails(3, "Life"));

         var col = new DataGridViewComboBoxColumn
         {
            DataSource = all_insurance_types,
            HeaderText = "Insurance Type",
            DataPropertyName = "InsurDetailz",
            DisplayMember = "ItType",
            ValueMember = "Self",
         };

         _myClient = new ClientInfo { 
            InsurDetailz = all_insurance_types[2], Name = "Jimbo" };
         grid.Columns.Add(col);
         grid.DataSource = new BindingList<ClientInfo> { _myClient };
         this.Controls.Add(grid);

         this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
      }
music-4580 2017-08-28
  • 打赏
  • 举报
回复
感谢回复 我来试试
xdashewan 2017-08-28
  • 打赏
  • 举报
回复
参考http://bbs.csdn.net/topics/390300264
exception92 2017-08-28
  • 打赏
  • 举报
回复
设置combobox的valuemember属性 为你数据库中的那个字段。
xdashewan 2017-08-28
  • 打赏
  • 举报
回复
引用 6 楼 lichunhui5231811 的回复:
这个保存的功能,是不是要通过SqlCommandBuilder才能实现??刚开始学这个 不懂的比较多。。有什么说的不明白 请包涵!
实现方式很多,SqlCommandBuilder也可以
music-4580 2017-08-28
  • 打赏
  • 举报
回复
引用 4 楼 only_endure 的回复:
DataGridViewComboBoxColumn DataSource? 问题: I'm trying to get something set up in a DataGridView. It seems like this should be pretty straightforward but I'm having trouble. I want to display three columns: CodeID CodeName ComboBox with DisplayMember of TypeName, ValueMember of TypeID I want to be able to select from all possible values of TypeName. Here's my dilemma: If I load all of this into one DataTable and set the DataGridView as the DataSource, I can display the existing TypeName for that record, but the combo box will not include any other values. If I set the DataSource for the DataGridViewComboBoxColumn to a separate DataTable that includes all possible TypeNames, the existing value is not displayed. DataGridView is really annoying to work with so either a solution for this or a viable alternative would be appreciated. Edit: it appears the issue is caused by my wanting to have a separate item for DisplayMember and ValueMember. The following works, if I don't worry about setting the ID as the ValueMember:
var typeColumn = new DataGridViewComboBoxColumn{
    DataSource = typeList,
    DisplayMember = "Type",
    ValueMember = "Type",
    DataPropertyName = "Type"}
If I do the following, the right types are selected, but I can't change the selection in the combo box:
var typeColumn = new DataGridViewComboBoxColumn{
    DataSource = typeList,
    DisplayMember = "Type",
    ValueMember = "TypeID",
    DataPropertyName = "TypeID"}
If I use the following I get a FormatException error as it's trying to populate:
var typeColumn = new DataGridViewComboBoxColumn{
    DataSource = typeList,
    DisplayMember = "Type",
    ValueMember = "TypeID",
    DataPropertyName = "Type"}
edit: typeList is a simple DataTable populated by the following: SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]FROM InsuranceType ITWHERE IT.ClientID = @ClientIDORDER BY [Type] 回答: Ok, I came up with an example ClientInfo and uranceDetails that I think might mimic what you are trying to do. Let me know if these details arent quite right. This example will populate the DataGridViewComboBox and set the value based on the InsuranceDetails specifically at: InsurDetailz = all_insurance_types[2])
 public partial class Form1 : Form
   {
      private ClientInfo _myClient;
      private BindingList<InsuranceDetails> all_insurance_types =
         new BindingList<InsuranceDetails>();

      public Form1()
      {
         InitializeComponent();

         DataGridView grid = new DataGridView();
         grid.Dock = DockStyle.Fill;
         grid.AutoGenerateColumns = true;

         all_insurance_types.Add(new InsuranceDetails(1, "Health"));
         all_insurance_types.Add(new InsuranceDetails(2, "Home"));
         all_insurance_types.Add(new InsuranceDetails(3, "Life"));

         var col = new DataGridViewComboBoxColumn
         {
            DataSource = all_insurance_types,
            HeaderText = "Insurance Type",
            DataPropertyName = "InsurDetailz",
            DisplayMember = "ItType",
            ValueMember = "Self",
         };

         _myClient = new ClientInfo { 
            InsurDetailz = all_insurance_types[2], Name = "Jimbo" };
         grid.Columns.Add(col);
         grid.DataSource = new BindingList<ClientInfo> { _myClient };
         this.Controls.Add(grid);

         this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
      }
感谢回复 正在努力的看您的解决方法
music-4580 2017-08-28
  • 打赏
  • 举报
回复
引用 5 楼 qq_38588710 的回复:
禁止datagridview的自动添加列,在设计器中添加项,绑定你要显示的字段,高效亲测
感谢回复!我之前试过,对combox列好像只有用代码处理,有具体例子吗?
music-4580 2017-08-28
  • 打赏
  • 举报
回复
引用 2 楼 xdashewan 的回复:
参考http://bbs.csdn.net/topics/390300264
参考这个帖子,那个下拉列表在datagridview中实现了,取数据库中的一个字段然后用sql语句改成所要显示的文字。现在有另外一个问题是这个combox列不仅仅是从数据库中取字段值然后显示在dataview的,还有一个取下拉列表值保存到数据库的一个功能。 这个保存的功能,是不是要通过SqlCommandBuilder才能实现??刚开始学这个 不懂的比较多。。有什么说的不明白 请包涵!

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧