我如何从数据表中选择 [英] how do i select from a data table

查看:82
本文介绍了我如何从数据表中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望组合框的选定索引更改事件从我的数据表中读取数据,并且
用结果填充列表框.我的代码似乎不起作用.任何帮助将不胜感激.
该代码在下面进行了详细说明:

 如果(mgDept.SelectedItem!= )
{
    searchresults rslts = mgDept.SelectedItem  as  searchresults;
    如果(rslts!= )
    {
        id = rslts.fileloc;
    }
}
exp = "  + id + " '";
DataRow [] foundrows = cvmanagerDataSet1.Tables [" ].Select(exp);
 for ( int  i =  0 ; i <  = foundrows.Length; i ++)
{
    角色ID = foundrows [i] [ 0 ].ToString();
    名称= foundrows [i] [ 2 ].ToString();
    mgRoles.Items.Add( searchresults(name,roleid));
} 

解决方案

从组合框索引更改事件获取值并传递Query的值. U获取值

我认为您使用.Select数据表是正确的……它通常比访问数据库快,并且具有附加的WHERE部分查询.

您可能需要检查数据...如果要比较字符串值,则必须匹配大小写...如果变量id的值为"Bob"而文件的值为"BOB"则将赢得此结果'' t匹配并返回您期望的值...因此,在执行.Select之前,您可能必须操纵外壳和/或修剪外壳.

如果那行不通,您能说不行来解释您的意思吗?您是否有任何要添加到mgRoles的值?它们是错误的值吗?


I want the selected index change event of a combo box to read data from my data table and
fill a listbox with the result. my code does not seem to work. any help will be much appreciated.
The code is detailed below:

if (mgDept.SelectedItem != null)
{
    searchresults rslts = mgDept.SelectedItem as searchresults;
    if (rslts != null)
    {
        id = rslts.fileloc;
    }
}
exp = "dept_id = '" + id + "'";
DataRow[] foundrows = cvmanagerDataSet1.Tables["role"].Select(exp);
for (int i = 0; i <= foundrows.Length; i++)
{
    roleid = foundrows[i][0].ToString();
    name = foundrows[i][2].ToString();
    mgRoles.Items.Add(new searchresults(name, roleid));
}

解决方案

Get the value form Combo box index change event and pass the value of Query. U get values


I think you are on the right track using the .Select for the datatable...it''s often faster than hitting the database and with the additional WHERE portion of the query.

You may want to check on the data...if you are comparing a string value you have to match the case...if your variable id has a value "Bob" and your file has "BOB" it won''t match and return values you are expecting...so you may have to manipulate the case and/or trim it before doing the .Select.

If that doesn''t work, could you please explain what you mean by saying it isn''t working? Are you getting any values to add into mgRoles? Are they the wrong values?


这篇关于我如何从数据表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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