如何将新项目添加到将绑定到数据库的组合框 [英] How to add an new item to a combobox which will bound to database

查看:53
本文介绍了如何将新项目添加到将绑定到数据库的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 嗨

在我的应用程序中,我使用的表称为Teacherdetailloading,它将绑定组合框

这是我的代码:

    combobox2.DataSource = ds.Tables["teacherdetailloading"];//ds is dataset
combobox2.DisplayMember = "name";
combobox2.ValueMember = "name";


如果我尝试向该组合框添加名为select的新项目,则会显示错误消息.

设置数据源后,无法修改项目集合.

我尝试过这个概念

//DataTable Dt1 = new DataTable();
             //Dt1.Columns.Add("district");
             //Dt1.Rows.Add("select");
             //Dt1.Merge(ds.Tables["teacherdetailloading"]);

             combobox2.DataSource = ds.Tables["Dt1"];
             combobox2.DisplayMember = "district";
             combobox2.ValueMember = "district";


但这是行不通的.如果有人有其他方法可以帮助我.

在此先感谢.

解决方案


您不能按照说明"消息中的说明将项目添加到已绑定数据源的控件中.合并DataTables很好.看一下这篇文章,其中有一个合并两个DataTable的示例(看这篇文章中的第一个解决方案):

如何合并两个数据集 [
然后在后面写绑定代码...

它可以正常工作....


绑定下拉列表后,请输入以下代码

combobox2.Items.Insert(0,new ListItem("Select","0"));


hi hi

in my application i am using table called teacherdetailloading which will to bound combobox

here is my piece of code:

    combobox2.DataSource = ds.Tables["teacherdetailloading"];//ds is dataset
combobox2.DisplayMember = "name";
combobox2.ValueMember = "name";


if i try to add new item called select to that combobox it will display an error message.

Item collection cannont be modified when the datasource is set.

i have tried this concepts

//DataTable Dt1 = new DataTable();
             //Dt1.Columns.Add("district");
             //Dt1.Rows.Add("select");
             //Dt1.Merge(ds.Tables["teacherdetailloading"]);

             combobox2.DataSource = ds.Tables["Dt1"];
             combobox2.DisplayMember = "district";
             combobox2.ValueMember = "district";


but it will not work. if anyone have any other way of doing it help me.

thanks in advance.

Hi,
You can not add item to a control which a datasource has been bound to as the Excaption message says. Merging DataTables is fine. Take a look at this post which has a sample of merging two DataTable (look at the first solution in the post):

how to merge two data set[^]

I hope it helps,
Cheers


select smart tag...

then add item text "select", manually in listitem collection editor...

then write binding code in behind...

it works gud....


After binding the dropdown type this code

combobox2.Items.Insert(0,new ListItem("Select", "0"));


这篇关于如何将新项目添加到将绑定到数据库的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆