Click here to Skip to main content
Sign Up to vote bad
good
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:48
Edited 5 Oct '12 - 19:08


3 solutions

You need to rename the column name of data-grid after bind with datatable
gvEmployee.columns[0].HeaderText=  "column name"
  Permalink  
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  
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 216
1 Richard MacCutchan 145
2 Sergey Alexandrovich Kryukov 129
3 Tadit Dash 129
4 Santhosh G_ 115
0 Sergey Alexandrovich Kryukov 10,338
1 OriginalGriff 7,965
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,159


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