English 中文(简体)
ComboBox的数据集
原标题:Filling of ComboBox with Dataset
  • 时间:2011-10-05 12:43:42
  •  标签:
  • c#

当试图填充Combo盒时

con.Open();
da = new SqlDataAdapter("Mt_Post_select",con);
//Mt_Post is Stored procedure with Select Command
ds1 = new DataSet();
da.Fill(ds1, "Mt_Post");
// The table has only one row
comboBox1.DataSource = ds1.Tables[0];
comboBox1.DisplayMember = "Mt_Post";

但是它没有数据显示它有<编码>System.Data.DataRowView。

该法典是错误的 请告诉我:

问题回答

替换Mt_Post,载于comboBox1.Display Members = “Mt_Post”;,其数值是你希望显示的栏目。

我希望以下样本有所帮助;

        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Email");
        dt.AcceptChanges();

        DataRow dr0 = dt.NewRow();
        dr0[0] = "Kadir Sumerkent";
        dr0[1] = "kadir@sumerkent.com";

        DataRow dr1 = dt.NewRow();
        dr1[0] = "Kadir Sumerkent 2";
        dr1[1] = "kadir@sumerkent2.com";

        dt.Rows.Add(dr0);
        dt.Rows.Add(dr1);
        dt.AcceptChanges();

        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Email";
        comboBox1.DataSource = dt;

让我们努力:

附录

SELECT 
     Users.User_name AS username
     Users.User_Code AS userID
FROM Users
-- WHERE CONDITONS ARE APPLIED

现在,为了<代码>Fetch,DataSet,你将做如下工作:


// YOUR OWN CODE
con.Open();
da = new SqlDataAdapter("Mt_Post_select",con);
//Mt_Post is Stored procedure with Select Command
ds1 = new DataSet();

// JUST FILL THE DataSet in THIS WAY
da.Fill(ds1);

// JUST TO MAKE SURE WE HAVE AN EMPTY COMBOBOX
comboBox1.Items.Clear();
comboBox1.DataSource = null;

// NOW POPULATE comboBox1 LIKE THIS
if (ds.Tables[0].Rows.Count  > 0)
{
  comboBox1.DataSource = ds1.Tables[0];
  comboBox1.DisplayMember = "username";
  comboBox1.ValueMember = "userID"
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签