C#中的下拉列表comboBox问题

weiaizoutianya11 2010-05-29 11:21:06
我加了this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;后,
SqlConnection conn = new SqlConnection(@"Data Source=./sqlexpress;Initial Catalog=Truck;Persist Security Info=True;User ID=sa;");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT PartNo, NewPart from AutoPart", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "PartNo";
comboBox1.ValueMember = "NewPart";
结果下拉列表显示为System.Data.DataRowView了,倘若System.Windows.Forms.DrawMode.Normal就可以。但是我要用的是上面的那种。该怎样让其正常显示呢
...全文
102 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesqsr 2010-05-29
  • 打赏
  • 举报
回复
参考哈 这个是我绑定的DropDownList
bluesqsr 2010-05-29
  • 打赏
  • 举报
回复
你这样写肯定是System.Data.DataRowView这个了。你直接帮他dataset就行了啊
string ddlsousuo = this.ddlsousuo.Text.Trim();
string txtss = this.txtss.Text.Trim();
string sqlstr = "select ID from NEWS where ClassID=(select ClassID from ClassES where ClassName=" + ddlsousuo + ") and Content like '%" + txtss + "%' or Title like '%" + txtss + "%' order by ID desc";
SqlConnection con = new SqlConnection(conn);
conn.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conn);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
this.ddlsousuo.DataSource=ds;
this.ddlsousuo.DataBind();
shighui 2010-05-29
  • 打赏
  • 举报
回复
adfadfafaf
s364021318 2010-05-29
  • 打赏
  • 举报
回复
路过。。。。。。。。。。。
zjtpiaoxue 2010-05-29
  • 打赏
  • 举报
回复
绑定到一个DataSet
comboBox1.DataSource = ds;
hhc123 2010-05-29
  • 打赏
  • 举报
回复 1
参考如下

private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
ListBox1.Items.AddRange(new Object[]
{ "Red Item", "Orange Item", "Purple Item" });
ListBox1.Location = new System.Drawing.Point(81, 69);
ListBox1.Size = new System.Drawing.Size(120, 95);
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
Controls.Add(ListBox1);
}

private void ListBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;

// Determine the color of the brush to draw each item based
// on the index of the item to draw.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}

// Draw the current item text based on the current Font
// and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
hhc123 2010-05-29
  • 打赏
  • 举报
回复
Normal 控件中的所有元素都由操作系统绘制,并且元素大小都相等。
OwnerDrawFixed 控件中的所有元素都是手动绘制的,并且元素大小都相等。
OwnerDrawVariable 控件中的所有元素都由手动绘制,元素大小可能不相等。

110,500

社区成员

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

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

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