Click here to Skip to main content
12,295,791 members (24,857 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I have predefined grid view it's contain two column (colEmpCode,colEmpName) those column name are assign by object properties

this is my code

OdbcCommand OpenEmp = new OdbcCommand("SELECT EMPLOYEE_GROUP_ASSIGN.EmpNo, EMPLOYEE_PERSONAL.FullName FROM " +  
                    "EMPLOYEE_GROUP_ASSIGN INNER JOIN EMPLOYEE_PERSONAL ON EMPLOYEE_GROUP_ASSIGN.EmpNo = EMPLOYEE_PERSONAL.EmpId " + 
                    "WHERE (EMPLOYEE_GROUP_ASSIGN.EmpGroupCode = '" + cmbEmpGrup.Text.Trim().ToString() + "')", MainMod.Conn);
                OdbcDataReader readEmp = OpenEmp.ExecuteReader();
                
                DataTable table = new DataTable();
                table.Columns.Add("EmpNo", typeof(string));
                table.Columns.Add("FullName", typeof(string));
                while (readEmp.Read())
                {
                    table.Rows.Add(MainMod.NullableString(readEmp.GetValue(0).ToString()),MainMod.NullableString(readEmp.GetValue(1).ToString()));
                }
                readEmp.Dispose();
                gvEmployee.DataSource = table;  

when i call (gvEmployee.DataSource = table) it give me record to new columns
in data grid view
but i want to add the records to my predefined columns like this

gvEmployee.columns[0].value = table.columns[0].value  // this is what i expting 
gvEmployee.columns[1].value = table.columns[1].value

how to achieve the target ?
Please help me
Posted 5-Oct-12 18:48pm
Edited 5-Oct-12 19:08pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need to rename the column name of data-grid after bind with datatable
gvEmployee.columns[0].HeaderText=  "column name"
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

it is not right,
gvEmployee.columns[0].value = table.columns[0].value

it should be,
gvEmployee.Columns[0].Datapropertyname = "EmpId";
// or 
//gvEmployee.Columns[0].Datapropertyname = table.columns[0].Name.ToString();

Happy coding!
Smile | :)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

If you want to manually fill datagridview I suggest to use strong typed datatables and rows from Dataset. In this case you don't have to worry about column names.
dapfor. com
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 1 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100