2

I have data access class which have List<> type method. In which data come from SQL server perfectly but I need to show it in my ASP GridView through iterate List<> data. This is my Data Access Layer.

   public class AdminPanelDataAccessLayer
{
    public List<PersonalInformation> GetAllPersonalInfo()
    {
        List<PersonalInformation> listsPersonalInfo = new List<PersonalInformation>();
        string connString = ConfigurationManager.ConnectionStrings["HRMS"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            SqlCommand cmd = new SqlCommand("usp_GetApplicantCNICAndName", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                PersonalInformation pi = new PersonalInformation();
                pi.CNIC = reader["CNIC"].ToString();
                pi.Name = reader["Name"].ToString();


                listsPersonalInfo.Add(pi);

            }
            return listsPersonalInfo;

        }
    }
}

And this is the method to show data in GridView through iteration.

        private void GetAllData()
    {
        AdminPanelDataAccessLayer apdal = new AdminPanelDataAccessLayer();
        List<PersonalInformation> pi = apdal.GetAllPersonalInfo();

        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("CNIC", typeof(string)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));

        foreach (object item in pi)
        {
            dr = dt.NewRow();
            dr["CNIC"] = pi.Select(o => o.CNIC);
            dr["Name"] = pi.Select(o => o.Name);
            dt.Rows.Add(dr);
        }

        ViewState["CurrentTable"] = dt;

        GridView2.DataSource = dt;
        GridView2.DataBind();
    }

Please Guide me to achieve my objective.

Aashir Haque
  • 153
  • 1
  • 2
  • 12

2 Answers2

4

Add you fields to the grid in your aspx:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
   <asp:BoundField DataField="CNIC" />
   <asp:BoundField DataField="Name" /> 
</asp:GridView>

Your Code Behind:

private void GetAllData()
{
    AdminPanelDataAccessLayer apdal = new AdminPanelDataAccessLayer();
    List<PersonalInformation> pi = apdal.GetAllPersonalInfo();

   //remove any records here, i am just removing first element here
    if (pi.Count > 0) 
        pi.RemoveAt(0);

    GridView2.DataSource = pi ;
    GridView2.DataBind();
}
Hussein Salman
  • 7,806
  • 15
  • 60
  • 98
  • I need to generate rows and columns from code by iterating List<> object and save it to GridView one by one – Aashir Haque Oct 18 '17 at 18:51
  • I am not sure what do you mean. Then why not bind the list directly to the Grid? and Add the Bound Fields with the corresponding Data Fields as indicated. – Hussein Salman Oct 18 '17 at 18:54
  • Ok I will do it but how to iterate List<> object data into this columns> – Aashir Haque Oct 18 '17 at 18:55
  • You don't need to iterate, it will automatically bind to it. Check the updated answer. – Hussein Salman Oct 18 '17 at 18:56
  • yes this will bind automatically but in my Senior I want to show some records of that sql table in gridview not all that is why i am not using automatic method – Aashir Haque Oct 18 '17 at 18:59
  • Then update your list and remove those records before binding it to the grid – Hussein Salman Oct 18 '17 at 19:00
  • I have 1 to many relation ship sql model and this table have primary key which is referencing a table (many relationship) and I need to show all record but this table is repeating because of (many relationship). that is why I need it to show once with other table records in same grid – Aashir Haque Oct 18 '17 at 19:06
  • 1
    This is another problem which is not related to this question here. – Hussein Salman Oct 18 '17 at 19:07
  • I just need to iterate List to track the grid's rows columns just like I want – Aashir Haque Oct 18 '17 at 19:10
0

Have you tried using the 'pi' List as your grid's datasource?

GridView2.DataSource = pi;
GridView2.DataBind();
  • yes this is working but this is auto generating rows and columns of GridView but I need to generate rows and columns of GridView by my self – Aashir Haque Oct 18 '17 at 18:45
  • You should be able to manually define the grid columns in the aspx page. – therea1wags Oct 18 '17 at 18:51
  • yes this will bind automatically but in my Senior I want to show some records of that sql table in gridview not all that is why i am not using automatic method – Aashir Haque Oct 18 '17 at 18:59