Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Team,

I have GridView on my page with following columns -
Emp_ID,
Emp_Name,
Emp_DOB

I have below code to refresh the same-
qry = "SELECT Emp_ID, Emp_Name, Emp_DOB FROM Employee_Master";
SqlCon.Open();
SqlDataAdapter adp = new SqlDataAdapter(qry, SqlCon);
System.Data.DataTable dt = new System.Data.DataTable();
adp.Fill(dt);
GridView1.DataSource = GetData();            
GridView1.DataBind();        
SqlCon.Close();


Problem : When I execute this - It will Create New Columns in GridView.
How I Want: I want to populate data to exesting columns only, which I have created in my gridview. This will help me to maintain the formatting such as Field Width, Wrap Text, etc.

Please help!!!!
Posted

1 solution

How I Want: I want to populate data to exesting columns only, which I have created in my gridview.
Set AutoGenerateColumns="false" and then define columns in your grid with mappings to the fields expected to be shown. This way, even if the datasource has new fields it will not affect the grid. Grid will bind only those fields that you have mentioned in your column mappings.

Look at the columns fields section here: MSDN: GridView Class[^]
 
Share this answer
 
Comments
Pravinkarne.31 14-May-12 2:25am    
Thank You So Much Mr. Sandeep!!!
This helped me a lot!!!
Sandeep Mewara 14-May-12 8:40am    
Welcome. :)

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