从datatable加载标签的text属性

时间:2012-04-09 20:15:11

标签: c#

我想从数据库中的值加载lsbel lbl约定的数量 我的代码是

public void Vehiclenocomboboxload()
       {

           OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
           oleDbConnection1.Open();

           OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("SELECT driverassignmastertable.drivername, driverassignmastertable.vehicleno, driverassignmastertable.amount, driverassignmastertable.driverpk FROM driverassignmastertable WHERE(((driverassignmastertable.jobcodepk)= @jobcodepk))", oleDbConnection1);
           oleDbCommand1.Parameters.AddWithValue("@jobcodepk", cmbjobcode.SelectedValue);
           OleDbDataReader reader = oleDbCommand1.ExecuteReader();
           DataTable dt = new DataTable();


           dt.Columns.Add("vehicleno", typeof(string));
           dt.Columns.Add("drivername", typeof(string));
           dt.Columns.Add("amount", typeof(int));
           dt.Columns.Add("driverpk", typeof(int));
           dt.Load(reader);
           if (dt.Rows.Count == 0)
           {
               MessageBox.Show("No vehicle Assigned to this Jobcode");

               cmbVehicleno.DataSource = null;
               txtvehicleno.Text = "";
           }
           else
           {

               cmbVehicleno.ValueMember = "driverpk";
               cmbVehicleno.DisplayMember = "vehicleno";
               lblagreeamount.Text = "amount"

               cmbVehicleno.DataSource = dt.DefaultView;



           }

           oleDbConnection1.Close();

       }

一切都很好,但我的问题是我的lblagreed数量没有从数据库获得任何价值 我的代码部分是  lblagreeamount.Text =“金额”

任何人都可以建议任何其他数据绑定方法

2 个答案:

答案 0 :(得分:1)

您尚未为标签指定值,只是静态“金额”:

lblagreeamount.Text = "amount"

您需要阅读DataTable第一行的相应字段:

lblagreeamount.Text = dt.Rows[0].Field<int>("amount").ToString();

答案 1 :(得分:0)

请使用此

lblagreeamount.Text = dt.Row[0]["amount"].ToString();