|
-
April 25th, 2008, 09:43 AM
#1
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!!
-
April 25th, 2008, 01:01 PM
#2
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!
-
April 25th, 2008, 02:24 PM
#3
Re: Datagridview
The tag is syntex error free at this point.
How to load a Datagridview in C# using ODBC.. thats bottomline.
Yes, Possible!!
-
April 25th, 2008, 02:35 PM
#4
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.
-
April 25th, 2008, 03:17 PM
#5
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!!
-
April 27th, 2008, 08:42 PM
#6
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;
}
-
April 28th, 2008, 08:18 AM
#7
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!!
-
April 28th, 2008, 01:08 PM
#8
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.
-
April 28th, 2008, 03:54 PM
#9
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!!
-
April 30th, 2008, 08:17 AM
#10
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!!
-
April 30th, 2008, 08:30 AM
#11
Re: Datagridview
Read the DW2 link in my signature, starting with the section Creating a Simple Data APplication
-
April 30th, 2008, 09:08 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|