Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add two list to one list. i am using linq to populate the list but it is showing some error. And i have to bind the new list contain two other list to grid view.

The follwoing is my code and it is working fine

XML
DataTable table;
        protected void Page_Load(object sender, EventArgs e)
        {
            bind();
        }
        public void bind()
        {
            SqlConnection conn = new SqlConnection(@"Data Source=CLAYSYSCH0028\Sharepoint;Initial Catalog=ShankarMS;Integrated Security=True");
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.CommandText = "select * from UserDetails";
            comm.Connection = conn;
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            DataSet set = new DataSet();
            adapter.Fill(set);
            table = set.Tables[0];
            //Stack<support> stack = new Stack<support>();
            List<int> first = new List<int>((from x in table.AsEnumerable() where x.Field<int>("ID") != 177 select x.Field<int>("ID")).ToArray());
            List<int> second = new List<int>((from x in table.AsEnumerable() where x.Field<int>("ID") == 180 select x.Field<int>("ID")).ToArray());
            List<int> main = new List<int>();
            if (second.Count > 0)
            {
                main.InsertRange(0, second);
                main.InsertRange(1, first);
            }
            else
            {
                main.InsertRange(0, first);
            }

            //stack.AddRange((from x in table.AsEnumerable() where x.Field<int>("ID") != 177 select x.Field<int>("ID")).ToList());
            grid.DataSource = main;
            grid.DataBind();
        }



This is my grid

XML
<asp:GridView ID="grid" runat="server" ClientIDMode="Inherit" >
        </asp:GridView>


SQL
But my requirment is i have to declare the first and second list as a custom class type.
My
custom class is

C#
public class support
   {
       public int id
       {
           get;
           set;
       }
       public string Name
       {
           get;
           set;
       }
   }


And i have to bind the new list main to grid. And i have to bind the values to template field in grid view.

And that grid is like this

XML
<asp:GridView ID="lst" runat="server" ClientIDMode="Inherit">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



Please help me.
Posted
Comments
Ziee-M 30-Oct-13 3:08am    
i dont think it is possible to bind diffrent class to the same grid, But you can inherit both of your classes from a parent class, and declare your both of them as parent class, and finally affect both of them to same Grid.

Son1 son1 = new Parent();
Son2 son2 = new Parent();

and datagrid will hold Parent object.
hope it helps

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900