Datagrid problem...

  • Thread starter Thread starter Morten Wennevik
  • Start date Start date
M

Morten Wennevik

Datagrids aren't what I know most about, but shouldn't you have a
gridPendings.DataBind();
to fill the datagrid with the dataset data?
 
That fills the dataset, but it doesn't fill the datagrid. You only tell
the datagrid that the datasource is ds, but you need to call DataBind() to
transfer data to the grid. I think.
 
Hi to all,

i think, this datagrid will drive me nuts...
I have the below code and i cannot figure out what is wrong and i cannot
format
the columns in the grid.
I, for example, try to format some things in "Sender" column but this is not
happening..


Can anyone give me a shot before i jump out of the window ??

thanks a lot for any help!


anthonyb




SqlConnection cnn = ...
cnn.Open();
SqlCommand cmd = new SqlCommand("Emails_FetchPending", cnn);
cmd.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(cmd);
AddGridStyle();

DataSet ds = new DataSet();
da.Fill(ds, "MyTable");
gridPendings.DataSource = ds;
gridPendings.DataMember = "MyTable";
cnn.Close();

private void AddGridStyle()
{
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "MyTable";
DataGridTextBoxColumn sender = new DataGridTextBoxColumn();
sender.MappingName = "Sender";
sender.HeaderText = "anthonyb";
ts.GridColumnStyles.Add(sender);
grid.TableStyles.Add(ts);
}
 
Morten Wennevik said:
Datagrids aren't what I know most about, but shouldn't you have a
gridPendings.DataBind();
to fill the datagrid with the dataset data?


But i use :
DataSet ds = new DataSet();
da.Fill(ds, "MyTable");

What is wrong with that ?
 
The grid displays correctly the data, but in an incorrect way...
Thanks anyway!
 
Works for me. Only noticed one error in the AddGridStyle function:

When loading the DataAdapter and setting the DataSource you reference
gridPendings...

gridPendings.DataSource = ds;
gridPendings.DataMember = "MyTable";

But in the AddGridStyle function, you reference a different grid...

grid.TableStyles.Add(ts);


If that's not the issue try playing with the TableStyle.MappingName. I've
had issues with this, but can't document it as a bug. I now use the same
string for all my table names. It seems resistant to change!

HTH,
Eric Cadwell
http://www.origincontrols.com
 
Anthony, how exactly do you do formatting?

this works fine for me (just make sure sender.MappingName == column name in the "MyTable" table):
sender.Format = "c"; //or "$0.000" and so on...
 
Eric you were right, that was indeed a problem with the code,
i wasn't referring to the correct grid.
But the problem really gon away when i used something like this:


ts.GridColumnStyles.Add(SetColumnStyle("EmailID", "EmailID",
LongestField((DataSet) gridSent.DataSource, "MyTable", gridSent,
"EmailID")));

gridSent.TableStyles.Add(ts);


And imagine doing this for every column in the grid...
I have worked in the past with third-party grids for vb6 and it was very
easy.
I see that in the datagrid you have even moe control on it,
but an "Autosize" property would make more sense...

thanks again!
 
Yes, I think syncfusion has a good autosize table routine.

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp

Tip 5.48. Download the demo and he has a function that loops the columns and
autosizes them all. Not sure if he handled hidden columns but that's an easy
fix.

Alternatively if you want the columns to fit the width of the grid, just
divide the (width of the grid minus the row header width) by the number of
visible columns (to hide a column set width to 0)

HTH,
Eric Cadwell
http://www.origincontrols.com
 
Back
Top