如何在c#中获取gridview的数据库值?

时间:2014-05-15 04:49:46

标签: c# asp.net gridview

selectdata.cs

    public List<BranchMaster> DisplayBranchMaster()
    {
        List<BranchMaster> Branchmaster = new List<BranchMaster>();
        try
        {
            Database db = new SqlDatabase(connectionstring);
            DbCommand cmd = db.GetStoredProcCommand("[dbo].[Display_BranchMaster1]");
            DataSet dsstatuses = db.ExecuteDataSet(cmd);

            for (int i = 0; i < dsstatuses.Tables[0].Rows.Count; i++)
            {
                BranchMaster status = new BranchMaster();
                //status.Branch_Id = Convert.ToInt32(dsstatuses.Tables[0].Rows[i]["Branch_id"]);
                status.Branch_Name = Convert.ToString(dsstatuses.Tables[0].Rows[i]["Branch_Name"]);
                status.Address = Convert.ToString(dsstatuses.Tables[0].Rows[i]["Address"]);
                status.City = Convert.ToString(dsstatuses.Tables[0].Rows[i]["city"]);
                status.Contact_Person = Convert.ToString(dsstatuses.Tables[0].Rows[i]["Contact_person"]);
                status.Email1 = Convert.ToString(dsstatuses.Tables[0].Rows[i]["Email"]);
                status.MobNo = Convert.ToString(dsstatuses.Tables[0].Rows[i]["MobNo"]);
                status.PhoneNo = Convert.ToString(dsstatuses.Tables[0].Rows[i]["PhoneNo"]);
                status.IsActive = Convert.ToBoolean(dsstatuses.Tables[0].Rows[i]["IsActive"]);
            }

            return Branchmaster;
        }
        catch (Exception ex)
        {
            return Branchmaster;
        }
    }

branchmaster.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Data;

 namespace Entity
 {
     public class BranchMaster
     {
         private int branch_Id;
         public int Branch_Id
         {
           get { return branch_Id; }
           set { branch_Id = value; }
         }
         private string branch_name;
         public string Branch_Name
         {
           get { return branch_name; }
           set { branch_name = value; }
         }
         private string Address1;
         public string Address
         {
           get { return Address1; }
           set { Address1 = value; }
         }
         private string city;
         public string City
         {
           get { return city; }
           set { city = value; }
         }
         private string contact_Person;
         public string Contact_Person
         {
           get { return contact_Person; }
           set { contact_Person = value; }
         }
         private string phoneNo;
         public string PhoneNo
         {
           get { return phoneNo; }
           set { phoneNo = value; }
         }
         private string mobNo;
         public string MobNo
         {
           get { return mobNo; }
           set { mobNo = value; }
         }
         private string Email;
         public string Email1
         {
           get { return Email; }
           set { Email = value; }
         }
         private bool isActive;
         public bool IsActive
         {
           get { return isActive; }
           set { isActive = value; }
         }
         public BranchMaster(DataRow data)
         {
           //branch_Id = Convert.ToInt32(data["branch_Id"]);
           branch_name = data["branch_name"].ToString();
           Address1 = data["Address1"].ToString();
           city = data["city"].ToString();
           contact_Person = data["contact_Person"].ToString();
           phoneNo = data["phoneNo"].ToString();
           mobNo = data["mobNo"].ToString();
           isActive = Convert.ToBoolean(data["isActive"].ToString());
         } 

addbranch.aspx.cs

    public DataTable GridBind()
    {
      DataSet ds1 = new DataSet();
      DataTable dt = new DataTable();
      dt.Columns.Add(new DataColumn("Branch Id ", typeof(int)));
      dt.Columns.Add(new DataColumn("Branch Name", typeof(string)));
      dt.Columns.Add(new DataColumn("Address", typeof(string)));
      dt.Columns.Add(new DataColumn("City", typeof(string)));
      dt.Columns.Add(new DataColumn("Contact person", typeof(string)));
      dt.Columns.Add(new DataColumn("Mobile No", typeof(string)));
      dt.Columns.Add(new DataColumn("Phone No", typeof(string)));
      //BranchMaster br = new BranchMaster();
      SelectData sl = new SelectData();
      List<BranchMaster> branchdetail = new List<BranchMaster>();
      branchdetail = sl.DisplayBranchMaster();

      for (int brm = 1; brm < branchdetail.Count; brm++)
      {
        DataRow dr = null;
        dr = dt.NewRow();
        dr["Branch Id"] = Convert.ToInt16(branchdetail[brm].Branch_Id);
        dr["Branch Name"] = Convert.ToString(branchdetail[brm].Branch_Name);
        dr["Address"] = Convert.ToString(branchdetail[brm].Address);
        dr["City"] = Convert.ToString(branchdetail[brm].City);
        dr["Contact person"] = Convert.ToString(branchdetail[brm].Contact_Person);
        dr["Mobile No"] = Convert.ToString(branchdetail[brm].MobNo);
        dr["Phone No"] = Convert.ToString(branchdetail[brm].PhoneNo);
        dt.Rows.Add(dr);
      }
      grdDisplayBranch.DataSource = dt;
      grdDisplayBranch.GridBind();
      return dt;
    }

这里我有3个文件selectdata.cs,branchmaster.cs class&amp; addbranch.aspx.cs。在selectdata.cs文件中,我从数据库中获取数据,它不会返回任何空值。并使用selectdata.cs文件数据,我在addbranch.aspx.cs文件中检索并显示数据顶级grideview但它没有显示任何内容。

1 个答案:

答案 0 :(得分:1)

在DisplayBranchMaster()的循环中,您忘记将状态对象添加到Branchmaster集合。

      status.MobNo = Convert.ToString(dsstatuses.Tables[0].Rows[i]["MobNo"]);
      status.PhoneNo = Convert.ToString(dsstatuses.Tables[0].Rows[i]["PhoneNo"]);
      status.IsActive = Convert.ToBoolean(dsstatuses.Tables[0].Rows[i]["IsActive"]);

      Branchmaster.Add(status);
   }
   return Branchmaster;

}