CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Datagridview

  1. #1
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Smile Datagridview

    Hi,

    Working on loading datagridview with the result from a stored procedure.
    The following piece of code does not throw any error but the datagridview shows nothing, its empty !!

    Any help is appreciated. I am sure someone out there , expert to help.

    --------------------------------------------------------------------
    OdbcConnection thisConnection = getConnection();

    OdbcCommand nonqueryCommand = thisConnection.CreateCommand();

    try

    {

    thisConnection.Open();

    nonqueryCommand.CommandText = "selectCategoryCode '83I'"; //selDafcMgrCode '83I'";

    DataSet ds = new DataSet();

    OdbcDataAdapter da = new OdbcDataAdapter();

    da.SelectCommand = new OdbcCommand(nonqueryCommand.CommandText, thisConnection);

    DataTable dTable = new DataTable();

    da.Fill(dTable);

    BindingSource bSource = new BindingSource();

    bSource.DataSource = dTable;

    d.DataSource = bSource;


    thisConnection.Close();

    }

    catch (OdbcException ex)

    {

    Console.WriteLine(ex.ToString());

    }
    ------------------------------------------------
    Thanks
    -dasm
    Yes, Possible!!

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Datagridview

    first off, please use code tags...
    second, you should modify your catch-block to include:
    Code:
    catch (Exception ex)
    {
             Console.WriteLine(ex.ToString());
    }
    and see if any other exceptions are caught...

    Other than that, I can't really offer much... I've never used stored procedures before...
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: Datagridview

    The tag is syntex error free at this point.

    How to load a Datagridview in C# using ODBC.. thats bottomline.
    Yes, Possible!!

  4. #4
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    Re: Datagridview

    You may want to explicitly tell your SqlCommand object that the CommandType is a StoredProcedure:

    Code:
    nonqueryCommand.CommandType = CommandType.StoredProcedure;
    I'm not sure what the default is though, so this may be unnecessary.

    Also, are you sure there's data in the DataTable after you execute the query? How is the SqlCommand object getting created? Are they reusable? I ask because we don't reuse any of ours.

    Code:
    try
    {
      thisConnection.Open();
      nonqueryCommand.CommandText = "selectCategoryCode '83I'";
      nonqueryCommand.CommandType = CommandType.StoredProcedure;
      OdbcDataAdapter da = new OdbcDataAdapter();
      da.SelectCommand = new OdbcCommand(nonqueryCommand.CommandText, thisConnection);
      DataTable dTable = new DataTable();
      da.Fill(dTable);
      BindingSource bSource = new BindingSource();
      bSource.DataSource = dTable;
      d.DataSource = bSource;
      thisConnection.Close();
    }
    catch (OdbcException ex)
    {
      Console.WriteLine(ex.ToString());
    }
    Actually, never mind... I apparently don't understand how OdbcCommand objects work...
    Last edited by opedog; April 25th, 2008 at 02:38 PM.

  5. #5
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: Datagridview

    Yes, the table has the data - in fact the datagridview shows the rows fetched, but w/o any data. I mean if the table has 2 rows , the datagridview shows two blank rows, if it has 3 then 3 blank rows.

    The code is little modified:

    --------------------------------
    try
    {
    thisConnection.Open();
    nonqueryCommand.CommandText = "selIdCodes '002'"; nonqueryCommand.CommandType = CommandType.StoredProcedure;
    OdbcDataReader thisReader = nonqueryCommand.ExecuteReader();

    DataSet ds = new DataSet();
    DataTable dTable = new DataTable("TableA");


    ds.Tables.Add(dTable);

    ds.Tables[0].Load(thisReader);
    d.DataSource = ds.Tables[0];

    thisConnection.Close();

    }
    catch (OdbcException ex)
    {
    Console.WriteLine(ex.ToString());
    }
    Yes, Possible!!

  6. #6
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Datagridview

    like the second post said;... use CODE TAGS, if you dont know what code tags are, you should read forums rules before posting.

    second,

    your code is setting up a datasource (bound to a datatable), but your datagridview is never set to that data.

    IE, quick dirty example of setting datagridview to a datasource:

    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable _Table = new DataTable("Table1");
                _Table.Columns.Add("Col1");
                _Table.Columns.Add("Col2");
                _Table.Rows.Add(new String[] { "Data", "Data" });
                _Table.Rows.Add(new String[] { "Data", "Data" });
    
                dataGridView1.DataSource = _Table;
            }

  7. #7
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: Datagridview

    Hi,

    Please see the comment - where it is loading and where it is binding the data to the datasource.
    But as i said it just does not show the data - but it shows the exact number of empty rows.. something wrong i am doing --not able to find out.. please help....



    HTML Code:
    try
    {
    	thisConnection.Open();
    	nonqueryCommand.CommandText = "selIdCodes '002'"; nonqueryCommand.CommandType = CommandType.StoredProcedure;
    	OdbcDataReader thisReader = nonqueryCommand.ExecuteReader();
    
    	DataSet ds = new DataSet();
    	DataTable dTable = new DataTable("TableA");
    
    
    	ds.Tables.Add(dTable);
    
    	ds.Tables[0].Load(thisReader); //this loads the data
    	d.DataSource = ds.Tables[0];   //this is binding the data to datagridview - d (d 
    is an object of datagridview)
    
    	thisConnection.Close();
    
    	}
    	catch (OdbcException ex)
    	{
    	Console.WriteLine(ex.ToString());
    }
    Last edited by dasm; April 28th, 2008 at 08:29 AM.
    Yes, Possible!!

  8. #8
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Datagridview

    there is a property called "AutoGenerateColumns". it is set to FALSE by default. Set it to TRUE.

    you aren't specifically giving columns(and names) to the datagridview. so it needs to create them itself.

  9. #9
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: Datagridview

    Hi eclipsed4utoo,

    Great!, that was the issue... It was false. When i set it to true , i was still not getting , because in the design mode i created the column in the datagridview. But once i set the property to true in the code , i removed all the columns(created in design view) and let the code generate the columns..
    So my final code piece is this: (if anyone likes to use !!)

    Code:
      
        public void execute(DataGridView d)
        {
            OdbcConnection thisConnection = getConnection();
            OdbcCommand nonqueryCommand = thisConnection.CreateCommand();
          try
            {
                thisConnection.Open();
                nonqueryCommand.CommandText = "storedprocedure 'parameter'"; 
                nonqueryCommand.CommandType = CommandType.StoredProcedure;
                OdbcDataReader thisReader = nonqueryCommand.ExecuteReader();
                
                DataSet ds = new DataSet();
                DataTable dTable = new DataTable("TableA");
                dTable.Load(thisReader);
                ds.Tables.Add(dTable);
                d.AutoGenerateColumns = true;
                d.DataSource = ds.Tables[0];
                thisConnection.Close();
                
            }
            catch (OdbcException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    Thanks a lot..
    Last edited by dasm; April 29th, 2008 at 07:35 AM.
    Yes, Possible!!

  10. #10
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    ComboBox

    Last week i got the DataGridView working, now i am getting almost simillar issue with my comboBox. The item count shows the exact number of rows fetched , but the combobox does not show any!!.
    Any help is appreciated..

    Code:
    public void loadComboBox(OdbcConnection con)
        {
            con = getConnection();
            con.Open();
            
            string str = "select distinct mgrCode from MgrDetail";
                 
            OdbcCommand nonqueryCommand = con.CreateCommand();
            nonqueryCommand.CommandText = str;
            nonqueryCommand.CommandType = CommandType.Text;
            OdbcDataReader thisReader = nonqueryCommand.ExecuteReader();
    
            DataSet ds = new DataSet();
            DataTable dTable = new DataTable("TableA");
            dTable.Load(thisReader);
            ds.Tables.Add(dTable);
            this.ComboBox1.DataSource = ds.Tables[0];
            int count = this.ComboBox1.Items.Count; //count gives exact number of rows !!
            
            con.Close();
    
        }
    Yes, Possible!!

  11. #11
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Datagridview

    Read the DW2 link in my signature, starting with the section Creating a Simple Data APplication
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  12. #12
    Join Date
    Apr 2008
    Location
    United States Of America
    Posts
    21

    Re: Datagridview

    that way we can configure at the design time.
    But what if we need to dynamically load the combobox when starting the application.(in the Form_load method).
    I wanted to load the combo box dynamically - because in that case i can upload aggregated result or whatever, instead of a straight column value from the underlying table!!
    Yes, Possible!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured