如何插入Databinded组合框 [英] How Do I Insert Into A Databinded Combo Box

查看:65
本文介绍了如何插入Databinded组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据结构组合框的索引零中插入新行,这是我的代码,但它似乎不起作用。



 使用(SqlCommand command =  new  SqlCommand( 从base_user_details中选择firstname,strCon))
{
command.CommandType = CommandType.Text;
// command.Parameters.Add(@ SelectOption,SqlDbType.VarChar,25).Value = staffFirstname;
// command.Parameters.Add(@ firstname,SqlDbType .VarChar,50)。Value = txtSearch.Text.Trim();
使用(SqlDataAdapter adapter = new SqlDataAdapter(command))
{
using (dt = new DataSet())
{
adapter.Fill(dt);
cboSchools.DataSource = dt.Tables [ 0 ];
cboSchools.ValueMember = firstname;
cboSchools.DisplayMember = firstname;

cboSchools.Items.Insert( 0 ---选择---);
}
}
}

解决方案

首先创建一个类,如



  public   class  ComboboxItem 
{
public string 文字{获得; set ; }
public object 值{ get ; set ; }

public 覆盖 string ToString()
{
return 文字;
}
}





添加代码以使用此类您想要添加项目



 ComboboxItem item =  new  ComboboxItem(); 
item.Text = Item text1;
item.Value = 12 ;
comboBox1.Items.Insert( 0 ,item);


试试这样:

 使用(dt =  new  DataSet())
{
adapter.Fill(dt);
cboSchools.DataSource = dt.Tables [ 0 ];
cboSchools.ValueMember = firstname;
cboSchools.DisplayMember = firstname;

cboSchools.Items.Add( ---选择--- 0 );
}


在设置数据源添加行到数据表之前执行以下操作

 adapter.Fill(DT); 
DataRow row = dt.Tables [ 0 ]。NewRow();
row.ItemArray = new object [] { - select - );
dt.Tables [ 0 ]。Rows.InsertAt(row, 0 );
cboSchools.DataSource = dt.Tables [ 0 ];
cboSchools.ValueMember = firstname;
cboSchools.DisplayMember = firstname;


How do i insert new row into index zero of a databinded combo box, here is my code, but it doesn't seem to work.

using ( SqlCommand command = new SqlCommand( "Select firstname from base_user_details", strCon ) )
                    {
                        command.CommandType = CommandType.Text;
                        //command.Parameters.Add( "@SelectOption", SqlDbType.VarChar, 25 ).Value = "staffFirstname";
                        //command.Parameters.Add( "@firstname", SqlDbType.VarChar, 50 ).Value = txtSearch.Text.Trim( );
                        using ( SqlDataAdapter adapter = new SqlDataAdapter( command ) )
                        {
                            using ( dt = new DataSet( ) )
                            {
                                adapter.Fill( dt );
                                cboSchools.DataSource = dt.Tables[0];
                                cboSchools.ValueMember="firstname";
                                cboSchools.DisplayMember ="firstname";
                                
                                cboSchools.Items.Insert( 0, "  ---Select---" );
                            }
                        }
                    }

解决方案

first create one class like

public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}



add code to use this class where you want to add item

ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;
comboBox1.Items.Insert(0,item);


Try like this :

using ( dt = new DataSet( ) )
                            {
                                adapter.Fill( dt );
                                cboSchools.DataSource = dt.Tables[0];
                                cboSchools.ValueMember="firstname";
                                cboSchools.DisplayMember ="firstname";
                                
                                cboSchools.Items.Add("---Select---",0 );
                            }


do as below, before set the data source add row to datatable

adapter.Fill(dt);
DataRow row = dt.Tables[0].NewRow();
row.ItemArray = new object[] { "--select--");
dt.Tables[0].Rows.InsertAt(row, 0);
cboSchools.DataSource = dt.Tables[0];
cboSchools.ValueMember="firstname";
cboSchools.DisplayMember ="firstname";


这篇关于如何插入Databinded组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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