473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

dataGridView help

Hi,

I have a dataGridView in my win Form, and i get some information from my
DataBase (sql) into a dataTable.

I tried and tried and i can't display my result in the dataGridView.

in addition, I also want to edit the columns (change the headr and size), i
know i can do it with the columns collection, but i need to bind each column
to a coulmn in my dataBase, and i don't know how to do it.

this is my code:

com = new SqlCommand();
da = new SqlDataAdapter();
// dt = new DataTable();

//
// com
//

this.com.CommandText = "[Get_Message_By_Person]";
this.com.CommandType = System.Data.CommandType.StoredProcedure;
this.com.Connection = m_mc.Get_dbcon().GetConn();
this.com.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@RETURN_VALUE" ,
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
false, ((System.Byte)(10)), ((System.Byte)(0)), "",
System.Data.DataRowVersion.Current, null));

//
// da
//
this.da.SelectCommand = this.com;

System.Data.SqlClient.SqlParameter msg_to =
this.com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@msg_to",
System.Data.SqlDbType.VarChar, 150));
msg_to.Value = employees_cb.Text;

da.Fill(msg_dt);

msg_dgv.DataSource = msg_dt;

thanks,
Gidi.
Oct 12 '07 #1
3 1726
The first thing I notice about your sample is that nowhere is the msg_dt
Datatable created before the Fill method is called. You need to have
DataTable msg_dt = new DataTable();
somewhere before you pass it into the Fill method. Some exception handling
would have also been helpful, as you would have seen the exception if you
caught it.
--Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Gidi" wrote:
Hi,

I have a dataGridView in my win Form, and i get some information from my
DataBase (sql) into a dataTable.

I tried and tried and i can't display my result in the dataGridView.

in addition, I also want to edit the columns (change the headr and size), i
know i can do it with the columns collection, but i need to bind each column
to a coulmn in my dataBase, and i don't know how to do it.

this is my code:

com = new SqlCommand();
da = new SqlDataAdapter();
// dt = new DataTable();

//
// com
//

this.com.CommandText = "[Get_Message_By_Person]";
this.com.CommandType = System.Data.CommandType.StoredProcedure;
this.com.Connection = m_mc.Get_dbcon().GetConn();
this.com.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@RETURN_VALUE" ,
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
false, ((System.Byte)(10)), ((System.Byte)(0)), "",
System.Data.DataRowVersion.Current, null));

//
// da
//
this.da.SelectCommand = this.com;

System.Data.SqlClient.SqlParameter msg_to =
this.com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@msg_to",
System.Data.SqlDbType.VarChar, 150));
msg_to.Value = employees_cb.Text;

da.Fill(msg_dt);

msg_dgv.DataSource = msg_dt;

thanks,
Gidi.
Oct 12 '07 #2
Hi,

I've the DataTable msg_dt = new DataTable(); i just put it in the Initialze
part and not in this part of code, so i still have these 2 problems.

Thanks,
Gidi.

"Peter Bromberg [C# MVP]" wrote:
The first thing I notice about your sample is that nowhere is the msg_dt
Datatable created before the Fill method is called. You need to have
DataTable msg_dt = new DataTable();
somewhere before you pass it into the Fill method. Some exception handling
would have also been helpful, as you would have seen the exception if you
caught it.
--Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Gidi" wrote:
Hi,

I have a dataGridView in my win Form, and i get some information from my
DataBase (sql) into a dataTable.

I tried and tried and i can't display my result in the dataGridView.

in addition, I also want to edit the columns (change the headr and size), i
know i can do it with the columns collection, but i need to bind each column
to a coulmn in my dataBase, and i don't know how to do it.

this is my code:

com = new SqlCommand();
da = new SqlDataAdapter();
// dt = new DataTable();

//
// com
//

this.com.CommandText = "[Get_Message_By_Person]";
this.com.CommandType = System.Data.CommandType.StoredProcedure;
this.com.Connection = m_mc.Get_dbcon().GetConn();
this.com.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@RETURN_VALUE" ,
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
false, ((System.Byte)(10)), ((System.Byte)(0)), "",
System.Data.DataRowVersion.Current, null));

//
// da
//
this.da.SelectCommand = this.com;

System.Data.SqlClient.SqlParameter msg_to =
this.com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@msg_to",
System.Data.SqlDbType.VarChar, 150));
msg_to.Value = employees_cb.Text;

da.Fill(msg_dt);

msg_dgv.DataSource = msg_dt;

thanks,
Gidi.
Oct 12 '07 #3

Hi Gidi,
I usually declare a dataset which houses a data table
that has field names and types the same as the selected fields in the
sproc.
I ask the dataadapter to fill the dataset's table then
pass the dataset back to the datagridview.
i.e.
void LoadData
{
myTypedDataset ds = myDataFillingFunction();
myDataGridView.DataSource = ds;
myDataGridview.DataMember = ds.myDataTable.TableName;
}

myTypedDataset myDataFillingFunction()
{
Connection stuff and parameter stuff
myTypedDataset d = new myTypedDataSet();
da.Fill(d.myDataTable);//intellsense will show you the table
return d;
}

As Peter said exception handling will allow you to see if the SPROC is
happy with your handiwork.

hth
Bob

On Fri, 12 Oct 2007 14:40:01 -0700, Gidi
<sh*****@hotmail.com.dontspamwrote:
>Hi,

I've the DataTable msg_dt = new DataTable(); i just put it in the Initialze
part and not in this part of code, so i still have these 2 problems.

Thanks,
Gidi.

"Peter Bromberg [C# MVP]" wrote:
>The first thing I notice about your sample is that nowhere is the msg_dt
Datatable created before the Fill method is called. You need to have
DataTable msg_dt = new DataTable();
somewhere before you pass it into the Fill method. Some exception handling
would have also been helpful, as you would have seen the exception if you
caught it.
--Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Gidi" wrote:
Hi,

I have a dataGridView in my win Form, and i get some information from my
DataBase (sql) into a dataTable.

I tried and tried and i can't display my result in the dataGridView.

in addition, I also want to edit the columns (change the headr and size), i
know i can do it with the columns collection, but i need to bind each column
to a coulmn in my dataBase, and i don't know how to do it.

this is my code:

com = new SqlCommand();
da = new SqlDataAdapter();
// dt = new DataTable();

//
// com
//

this.com.CommandText = "[Get_Message_By_Person]";
this.com.CommandType = System.Data.CommandType.StoredProcedure;
this.com.Connection = m_mc.Get_dbcon().GetConn();
this.com.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@RETURN_VALUE" ,
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
false, ((System.Byte)(10)), ((System.Byte)(0)), "",
System.Data.DataRowVersion.Current, null));

//
// da
//
this.da.SelectCommand = this.com;

System.Data.SqlClient.SqlParameter msg_to =
this.com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@msg_to",
System.Data.SqlDbType.VarChar, 150));
msg_to.Value = employees_cb.Text;

da.Fill(msg_dt);

msg_dgv.DataSource = msg_dt;

thanks,
Gidi.
Oct 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wellington Eliel Lopes | last post by:
Hi people, I have a DataGridView with 3 columns of DataGridViewComboboxColumn type. I need select a different value in the first combobox cell and refresh others columns in relation the first...
10
by: Henok Girma | last post by:
Hello Gurus, I want to save the state of an unbound DataGridView on my Windows Form application to an XML file so i can later load it back.. Basically, on my form I have a DataGridView, it's got...
11
by: Kevin | last post by:
I've been searching forever for examples of saving data changes in a DataGridView. There's all kinds of examples, but none really show how to save changes. Someone please help me. I have a...
1
by: mattioli.mirko | last post by:
Hello,I'm a problem. I inizialize an object BindingSource with : myBindingSource.DataSource = myDatatable; and myDataGridView.DataSource = myBindingSource. but I have a problem : when I filter...
1
by: teddysnips | last post by:
I come from a VB background and I'm on my first C# project. I have a panel with a DataGridView control. When the program is first run, the panel is instantiated with 4,000 empty rows (this is a...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
1
by: trint | last post by:
Hi, I need to add a cell to my dataGridView that will contain 4 radio buttons and another cell that contains 3 textboxes. Any examples of this would be appreciated. Thanks, Trint
1
by: enrico via DotNetMonster.com | last post by:
can you input a field in a datagridview that can be save to your database? can you make a calculation on it? please tell me how. -- Message posted via DotNetMonster.com...
1
by: Tony M | last post by:
VS 2005 - vb .net - windows xp pro - windows app All I'm trying to do for now is populate a datagridveiw (dgvTest). I trying to learn how to retrieve records from DB and add, edit, delete. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.