我正在使用listbox ownerdrawfixed但返回system.data.datarowview [英] I'm using listbox ownerdrawfixed but returns system.data.datarowview

查看:81
本文介绍了我正在使用listbox ownerdrawfixed但返回system.data.datarowview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我在我的列表框中使用ownerdrawfixed,它被数据绑定到数据库。我使用ownerdrawfixed因为我需要在列表框的行中有特定的颜色响应。在我的Listbox4.DrawItem方法中,我有以下代码:

I'm using ownerdrawfixed in my listbox which is databound to a database. I'm using ownerdrawfixed because I need to have a specific colour response in the rows of the Listbox. In my Listbox4.DrawItem method I have this code:

                e.DrawBackground();

                using(Brush textBrush = new SolidBrush(e.ForeColor))

                ; {

                    //e.Graphics.DrawString(listBox7.Items [e.Index] .ToString(),e.​​Font,textBrush,e.Bounds.Location);

                    


                    e.Graphics.DrawString(listBox4.Items [e.Index] .ToString(),e.​​Font,textBrush,e.Bounds);
$
                     //listBox4.SelectedValue = listBox4.Items [e.Index] .ToString();

             ;       


                }

                e.DrawBackground();
                using (Brush textBrush = new SolidBrush(e.ForeColor))
                {
                    //e.Graphics.DrawString(listBox7.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds.Location);
                   
                    e.Graphics.DrawString(listBox4.Items[e.Index ].ToString(), e.Font, textBrush, e.Bounds);
                    //listBox4.SelectedValue = listBox4.Items[e.Index].ToString();
                   
                }

但是不是显示数据库的值,而是在Listbox的每一行都得到System.Data.DataRowView。

But instead of values of the database being shown I get System.Data.DataRowView in every row of the Listbox.

任何人都可以帮忙吗? 。

Can anyone help please.

推荐答案

那是因为你在DataRowView上调用ToString()。在大多数情况下,类型不会覆盖ToString方法,因此您只需获取类型名称,就像在此处所做的那样。在您发布的代码中,您将获得listBox4.Items [e.Index]。由于你没有发布
对该字段的赋值,我认为它是一个DataRowView,它来自你用来绑定它的DataTable。

That's because you're calling ToString() on a DataRowView. In most cases, a type doesn't override the ToString method so you simply get the type name, as is going on here. In your posted code you're getting listBox4.Items[e.Index]. Since you didn't post the assignment to that field I'm assuing it is a DataRowView that came from the DataTable you used to bind to it.

列表框有几个可以设置的属性。
SelectedItem
是选中的实际值(DataRow)。当您需要访问基础对象时,可以使用此属性。你不应该认为它是一个字符串(它不在这里)。

SelectedValue
来自基本类型并代表"价值"。该项目。这通常不是项目本身。一些控件还具有SelectedText,其是"文本"。该项目的版本。这通常是
渲染的控件。

A Listbox has several properties that you can set. SelectedItem is the actual value (DataRow) that is selected. You use this property when you need access to the underlying object. You shouldn't assume it is a string (which it isn't here). SelectedValue is coming from the base type and represents the "value" of the item. This is often not the item itself. Some controls also have a SelectedText which is the "text" version of the item. This is what the control generally renders.

如果你正在使用数据绑定(通过DataSource),那么你可以设置
DisplayMember
属性对象的属性表示你想要的要显示的列表框。对于DataRow,这是包含要显示的文本的列。

ValueMember
可用于设置"值"。排。对于DataRow,这通常是行的某种唯一ID,例如PK。

If you're using databinding (via DataSource) then you can set the DisplayMember property to the property on the object that represents what you want the listbox to show. For a DataRow this is the column containing the text to be displayed. ValueMember can be used to set the "value" of the row. For a DataRow this is generally some sort of unique ID of the row such as the PK.

因此,总而言之,使用DataSource来设置要呈现的DataTable(或行)。将DisplayMember设置为要显示的列。将ValueMember设置为包含唯一ID的列。然后控件将正确呈现。要获取基础数据
,请使用SelectedItem。要获取所选值(例如ID),请使用SelectedValue。

So, in summary, use DataSource to set the DataTable (or rows) to render. Set DisplayMember to the column that you want to display. Set ValueMember to the column that contains the unique ID. Then the control will render correctly. To get the underlying data use SelectedItem. To get the selected value (e.g. ID) then use SelectedValue.


这篇关于我正在使用listbox ownerdrawfixed但返回system.data.datarowview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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