C#从Access数据库和ComboBox将字符串转换为Int [英] C# Converting String to Int from Access Database and ComboBox

查看:316
本文介绍了C#从Access数据库和ComboBox将字符串转换为Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将数据库中的变量更改为可用的Int,因此我可以与数据库进行比较以删除行。



这里是我的代码使用:

  private void EditEmployee_Load(object sender,EventArgs e)
{
DataTable table = new DataTable (); //创建一个表以在填充ComboBox之前存储数据
string connstring = @Provider = Microsoft.ACE.OLEDB.12.0; Data Source = | DataDirectory | \\HoliPlanData.accdb; Persist Security Info =假;
try
{
using(OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
string query =SELECT PayrollNo,(FirstName +''+ LastName)AS NAME FROM [Employee];
OleDbDataAdapter adapter = new OleDbDataAdapter(query,conn);
adapter.Fill(table);
DropBoxEmp.DataSource = table;
DropBoxEmp.DisplayMember =NAME;
DropBoxEmp.ValueMember =PayrollNo;
string BadNumber = DropBoxEmp.ValueMember;
int GoodNumber = Int32.Parse(BadNumber);

}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());问题是,字符串BadNumber设置为字符串PayrollNo,而不是字符串而不是Datatable将PayrollNo存储为的Int的字符串版本。当我将DisplayMember设置为PayrollNo时,组合框显示1和2,依此类推,作为在SELECT查询中选择的Int。



为什么BadNumber采用字面分配给DropBoxEmp.ValueMember而不是数字?

解决方案

您想要 DropBoxEmp.SelectedItem ,而不是 ValueMember 。你误解了 ValueMember 属性是什么。



要执行此操作,请执行以下操作:

  var theItem = DropBoxEmp.SelectedItem as DataRowView; 
MessageBox.Show(theItem [0] .ToString());

- 另外,数据适配器将打开连接,因此您不需要


I'm struggling to change a variable from a database into a usable Int so I can compare against the database to delete rows.

Here's the code I'm using:

private void EditEmployee_Load(object sender, EventArgs e)
    {
        DataTable table = new DataTable(); //creates a Table to store the data before populating the ComboBox
        string connstring = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";
        try
        {
            using (OleDbConnection conn = new OleDbConnection(connstring))
            {
                conn.Open();
                string query = "SELECT PayrollNo, (FirstName + ' ' + LastName) AS NAME FROM [Employee]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
                adapter.Fill(table);
                DropBoxEmp.DataSource = table;
                DropBoxEmp.DisplayMember = "NAME";                    
                DropBoxEmp.ValueMember = "PayrollNo";
                string BadNumber = DropBoxEmp.ValueMember;
                int GoodNumber = Int32.Parse(BadNumber);

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

The problem is that the string BadNumber is set to the string "PayrollNo", instead of a string version of the Int that the Datatable stores PayrollNo as. When I set the DisplayMember to "PayrollNo", the combobox displays 1 and 2 and so on, as the Int's selected in the SELECT query.

Why does BadNumber take the literal assigned to DropBoxEmp.ValueMember instead of the numbers? Any help would be gratefully received.

解决方案

You want DropBoxEmp.SelectedItem, not ValueMember. You are misunderstanding what the ValueMember property is. It tells the combo box which field in its binding to use as the backing value.

To unbox, do this:

var theItem = DropBoxEmp.SelectedItem as DataRowView;
MessageBox.Show(theItem[0].ToString());

-- as an aside, the data adapter will open the connection so you don't need to do that.

这篇关于C#从Access数据库和ComboBox将字符串转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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