0

Below is my Datagridview code to get the data from employee table.

the problem am facing is ,my employee table have 10 columns (ID,emplNo,Dob,JoingData...etc)

i just want to fill my grid with only ID,EmplyNo and DOB.

but the below code get everything,please advise me what i suppose to do to get only particular column

          string sql = "select * from Employee";
        SqlConnection connection = new SqlConnection(CONNECTION_STRING);
        //SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
          dataadapter = new SqlDataAdapter(sql, connection);
       // DataSet ds = new DataSet();
        ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, scrollVal, 5, "Employee");
        connection.Close();
        dgMessages.DataSource = ds;
        dgMessages.DataMember = "tEmployee";
Usher
  • 2,146
  • 9
  • 43
  • 81

2 Answers2

3

instead of

   string sql = "select * from Employee";

do

   string sql = "select ID, EmplyNo, DOB from Employee";
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
1

either change your select to only get the supset of what you want or use the designer: enter image description here enter image description here

it's a bit of a pain because you first have to add an unbound column and then edit the just added column to setthe DataPropertyName to a column-name in your resulting table - but it works.

You find this dialogs by clicking the "..." in the "columns" property of the PropertyEditor for the DataGridView or by clicking the little "Play"-Button on the top-right of the Grid in the designer (when it is selected)

Almost forgot: IMPORTANT: you need to add EVERY column and set Visible=False on those you don't want to see - I think this is different in WebForms where there is something like AutogenerateColumns.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • I think you missed something, because although you design 3 columns, in the runtime, if the `DataSource` having 4 or more columns, it will display all, unless you handle it with `grid.Columns[name].Visible = false` – Tu Tran Aug 19 '11 at 07:49
  • well read the last 2 lines ... have to admit I forgot it in the first go but I added those lines a short time after. I don't use WinForms anymore nor do I WebForms so I tend to forget where the AutoGenerate-bla stuff was. – Random Dev Aug 19 '11 at 07:53
  • thanks CKoenig, actuall i added the column and one of my column is hyperlink,now the problem is how to add the value to the coulum. – Usher Aug 19 '11 at 08:05
  • you see the "DataPropertyName" in the "Data"-Section of the property-editor above? There you have to put the name of your column in the DataTable - in your case "EmplyNo" for example (without the "" of course) BTW: you don't have to use a dataset if you only need the DataTable! This way you might have to say "tEmployee.EmplyNo" but I don't think so) – Random Dev Aug 19 '11 at 08:08
  • i was trying to do something like below datagridview.Rows[n].Cells[0].Value = EmpId something like that but am not sure how to do. – Usher Aug 19 '11 at 08:10
  • have a look at that question: http://stackoverflow.com/questions/7092391/c-winform-datagridview-inline-add-new-row/7092426#7092426 it's almost the same. If not please ask again - this is seperate from your question above – Random Dev Aug 19 '11 at 08:17
  • this was the actual code i was doing to pull the message from my Q and load it in to my datagridview column,now i am planning to do the same for sql table. you mentioned i have to use datatable but i couldn't find any examples but found only dataset.here is my code – Usher Aug 19 '11 at 08:20
  • ErrGrid.Rows[n].Cells[1].Value = "123XXX"; ErrGrid.Rows[n].Cells[2].Value = "Here am trying to load my employee no from sql table"; ErrGrid.Rows[n].Cells[3].Value = "MessageType"; ErrGrid.Rows[n].Cells[4].Value = "Sender"; ErrGrid.Rows[n].Cells[5].Value = msgInfo[msgInfo.Length - 1]; ErrGrid.Rows[n].Cells[6].Value = m.ToString(); – Usher Aug 19 '11 at 08:22