Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I m using C# .net with Access DB.I m trying to retrieve data from DB to Data grid view to display data but it is showing only 1st and 2nd column remaining are not showing data but in dataset it is there ,can any one help please?

Below I give code

private void grid()
       {
           dgv.Columns.Clear();
           DataGridViewTextBoxColumn dgvCol = new DataGridViewTextBoxColumn();
           string str;
           str = "SELECT * FROM excess";
           OdbcCommand cmd = new OdbcCommand(str,con);
           OdbcDataAdapter da = new OdbcDataAdapter();
           DataSet ds = new DataSet();
           da.SelectCommand = cmd;
           da.Fill(ds, "ExcessGView");

           if (ds.Tables["ExcessGView"].Rows.Count > 0)
           {
               foreach (DataRow dr in ds.Tables["ExcessGView"].Rows)
               {
                   for (int i = 0; i < ds.Tables["ExcessGView"].Columns.Count; i++)
                   {
                       dgvCol = new DataGridViewTextBoxColumn();
                       dgvCol.Width = 150;
                       dgvCol.ReadOnly = true;
                       dgvCol.SortMode = DataGridViewColumnSortMode.Programmatic;
                       dgvCol.HeaderText = ds.Tables["ExcessGView"].Columns[i].ColumnName.ToUpper();
                       dgv.Columns.Add(dgvCol);
                   }
                  break;
               }

               int r = 0;
               dgv.RowCount = ds.Tables["ExcessGView"].Rows.Count;

               foreach (DataRow dr in ds.Tables["ExcessGView"].Rows)
               {
                   for (int i = 0; i < ds.Tables["ExcessGView"].Rows.Count; i++)
                   {
                       try
                       {
                           dgv.Rows[r].Cells[i].Value = dr[i].ToString();
                       }
                       catch (Exception ex)
                       {
                           con.Close();
                           MessageBox.Show(ex.Message);
                       }
                   }
                   r = r + 1;
               }

           }

           else
           {
               con.Close();
           }

       }



Thanks & regards
Indrajit

[Edited]Code is blocked in "pre" tags[/Edited]
Posted
Updated 3-May-11 19:06pm
v2

Hi,


Use this simple syntax to show the data

dgv.DataSource=ds.Tables["ExcessGView"];

instead of these loops.
Try it.

Regards
AR
 
Share this answer
 
v2
Comments
IndrajitDasgupat 4-May-11 2:01am    
Thanks it is working
IndrajitDasgupat 4-May-11 2:12am    
how I can make the size of every column of data grid view according to user, please help
Ankit Rajput 4-May-11 2:22am    
set this property of grid
AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells;
IndrajitDasgupat 4-May-11 2:36am    
Yes I had done like below
//the DataGridView
DataGridView dgv = new DataGridView();
dgv.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells;

dgv.Columns("excess").Width = 270;
dgv.Columns("excess").DisplayIndex = 1;
dgv.Columns("excess").ReadOnly = True;
dgv.Columns("excess").HeaderText = "Excess";
but in column it is showing error that it is looks like method
what should i do?
please help!
Ankit Rajput 4-May-11 2:45am    
You have used like
dgv.Columns("excess").Width = 270;

but use it like
dgv.Columns["excess"].Width = 270;

Means use '[]' in place of '()' when accessing columns.
Just do two things:
1. Keep AutoGenerateColumns = true for the datagrid
2. Use Datagrids DataSource property and then bind it, like:
mydg.Datasource = ds.Tables["mytable"];
mydg.Databind();
 
Share this answer
 
Comments
Ankit Rajput 4-May-11 1:12am    
I think, we need to call DataBind function in case of web application only. Am i right?
Sandeep Mewara 4-May-11 1:25am    
Right you are!

My bad, I am more of ASP.NET guy and to me by default it looked like a webquestion. Since it is not mentioned if it is a web or winforms, I am at a little loss/advantage. :)

Ankit Rajput 4-May-11 2:17am    
Actually its my confusion thats why i asked.
Sandeep Mewara 4-May-11 2:21am    
No, you were right. For winforms, we don't need Databind() as we do in ASP.NET.
Ankit Rajput 4-May-11 2:23am    
Thanks, For make it clear.

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