dataGridView help

G

Guest

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.
 
G

Guest

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
 
G

Guest

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 said:
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 said:
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.
 
B

bob clegg

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

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 said:
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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top