Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have predefined grid view it's contain two column (colEmpCode,colEmpName) those column name are assign by object properties

this is my code

C#
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

C#
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
Updated 5-Oct-12 19:08pm
v2

You need to rename the column name of data-grid after bind with datatable
VB
gvEmployee.columns[0].HeaderText=  "column name"
 
Share this answer
 
v2
it is not right,
C#
gvEmployee.columns[0].value = table.columns[0].value


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


Happy coding!
:)
 
Share this answer
 
VB
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
 
Share this answer
 

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