简体   繁体   English

从适配器检索特定项目?

[英]Retrieve a specific item from an adapter?

I've got this block of code in C# code-behind that fills a combo box: 我在C#代码中获得了以下代码块,该代码块填充了一个组合框:

ddlRole.Items.Clear();
ddlRole.SelectedValue = null;
DataTable DDLRoles = new DataTable();
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["CLTDPL"].ConnectionString))
{
    //SqlDataAdapter adapter = new SqlDataAdapter("Select [Role_ID], [Role] from [MOS_Role] where [Function_ID] = " + FunID + " ORDER BY [Role] ASC", con2);
    SqlDataAdapter adapter = new SqlDataAdapter("Select [Role_ID], [Role], [BILLABLE_LBL] from [MOS_Role] where [Function_ID] = @FunID ORDER BY [Role] ASC", con2);
    adapter.SelectCommand.Parameters.AddWithValue("@FunID", Convert.ToInt32(ddlFunction.SelectedValue));
    adapter.Fill(DDLRoles);
    ddlRole.DataSource = DDLRoles;
    ddlRole.DataTextField = "Role";
    ddlRole.DataValueField = "Role_ID";
    ddlRole.DataBind();

}
ddlRole.Items.Insert(0, new ListItem("Select your role", "0"));

Now I'm being asked to change a label based on the field [BILLABLE_LBL]. 现在,系统要求我根据[BILLABLE_LBL]字段更改标签。 So, I need to check if that field is NULL or 0, and write an If/Else statement based on that. 因此,我需要检查该字段是否为NULL或0,并基于该字段编写If / Else语句。 However, I can't figure out how to get that value. 但是,我不知道如何获得该价值。

I mean, I could make a separate call to the data and use a reader, but that can't be the most efficient way. 我的意思是,我可以单独调用数据并使用阅读器,但这不是最有效的方法。

I hope I understand the question correctly but you want to change a field according to the selected Role from the combo box and don't know how to get the [BILLABLE_LBL] value (feel free to correct me). 我希望我正确理解了这个问题,但是您想根据下拉列表中的选定角色来更改字段,并且不知道如何获取[BILLABLE_LBL]值(随时可以纠正我)。

You can get the DataRow from your DataTable via the Select() method. 您可以通过Select()方法从DataTable获取DataRow

DataRow[] row = DDLRole.Select("Role_ID = '" + selectedValueFromCombobox + "'");

Now that you have your matching DataRow you can read out the desired value: 现在您有了匹配的DataRow您可以读出所需的值:

string labelText = (row[0]["BILLABLE_LBL"] != DBNull.Value && row[0]["BILLABLE_LBL"] != 0) ? (string)row[0]["BILLABLE_LBL"] : "Empty Field";

This call checks if the [BILLABLE_LBL] is Null or 0 like you wanted. 该调用检查[BILLABLE_LBL]是否为Null或0(如您所愿)。 If it is not Null or 0, it takes the value and saves it as string labelText . 如果它不是Null或0,它将获取该值并将其另存为string labelText Else you can set an alternative text like "Empty Field" or just ""; 另外,您可以设置一个替代文本,例如“ Empty Field”或“”。

I assume you have a Listener on your ComboBox to check when the value changes, or a "Commit Button" or some other method where you retrieve the selected Role value. 我假设您在ComboBox上具有一个Listener来检查值何时更改,或者是一个“ Commit Button”或用于检索所选Role值的其他方法。 Just add the code to your method/listener to get your desired value and set it as text for your label. 只需将代码添加到您的方法/侦听器中即可获得所需的值,并将其设置为标签的文本。


EDIT : Maybe this is more what you were looking for, since you don't need to use Select() on the datatable: 编辑 :也许这是您想要的,因为您不需要在数据表上使用Select()

DataRowView selectedRow = (ddlRole.SelectedItem as DataRowView);

To retrieve values from the row you can just use: selectedRow["BILLABLE_LBL"] Now you can do your check for Null and 0 and you are good to go. 要从该行中检索值,您可以使用: selectedRow["BILLABLE_LBL"]现在,您可以检查Null和0,这很好。 I don't know which one is more efficient (Select or ... as DataRowView) but either way you get your value. 我不知道哪种方法更有效(选择或...作为DataRowView),但是无论哪种方式都能获得价值。

(Credit for the .SelectedItem as DataRowView to https://stackoverflow.com/a/9825085/5332988 ) (将.SelectedItem as DataRowView信用.SelectedItem as DataRowView信用.SelectedItem as DataRowViewhttps://stackoverflow.com/a/9825085/5332988

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM