DataGridView

R

ReneMarxis

Hello

i have some simple question on DataGridView.

I use a DataGridView to display some data (databound) and can't get 2
things working:
1) i want to show just some of the columns not all
2) i want to change the colum-header text
2) i want to rearrange the columns

For 1) and 2) i counld just set the Column.Visible flag to false and
use the HeaderText property (which works)
But i can t get point 3) running.

I thought of using a DataView for solving all 3 problem but it seems
like i misunderstood the meaning of that DataView object :)

Heres the code i use to fill the grid:

DBLists dbLists = new DBLists();
Kunden kdList = new Kunden();
DataSet ds;
BindingSource bsKunden;
DataView dv

ds = new DataSet();
kdList.oleDA.Fill( ds, "Kunden" ); //use the DataAdapter
bsKunden = new BindingSource( ds, "Kunden" );
dgKunden.DataSource = bsKunden;

dv = new DataView( ds.Tables["Kunden"] );
bsKunden.DataSource = dv;
dgKunden.DataSource = bsKunden; //set the DataSource of the grid

Can anyone give me some hints? At least for the 3) problem.
PS: I can't/don't want to change the select statement of the
DataAdapter, as it is used often in the project but i need to display
the data in different order.

_thanks in advance rene
 
N

Nicholas Paldino [.NET/C# MVP]

ReneMarxis,

If you want to display the columns in a particular order on the
DataGridView, then you are going to have to add the columns manually to the
DataGridView, and not let the grid automatically generate the columns for
you (you could also choose while manually adding the columns to the grid
which columns to display, and which not to).
 
R

ReneMarxis

ReneMarxis,

If you want to display the columns in a particular order on the
DataGridView, then you are going to have to add the columns manually to the
DataGridView, and not let the grid automatically generate the columns for
you (you could also choose while manually adding the columns to the grid
which columns to display, and which not to).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


i have some simple question on DataGridView.
I use a DataGridView to display some data (databound) and can't get 2
things working:
1) i want to show just some of the columns not all
2) i want to change the colum-header text
2) i want to rearrange the columns
For 1) and 2) i counld just set the Column.Visible flag to false and
use the HeaderText property (which works)
But i can t get point 3) running.
I thought of using a DataView for solving all 3 problem but it seems
like i misunderstood the meaning of that DataView object :)
Heres the code i use to fill the grid:
DBLists dbLists = new DBLists();
Kunden kdList = new Kunden();
DataSet ds;
BindingSource bsKunden;
DataView dv
ds = new DataSet();
kdList.oleDA.Fill( ds, "Kunden" ); //use the DataAdapter
bsKunden = new BindingSource( ds, "Kunden" );
dgKunden.DataSource = bsKunden;
dv = new DataView( ds.Tables["Kunden"] );
bsKunden.DataSource = dv;
dgKunden.DataSource = bsKunden; //set the DataSource of the grid
Can anyone give me some hints? At least for the 3) problem.
PS: I can't/don't want to change the select statement of the
DataAdapter, as it is used often in the project but i need to display
the data in different order.
_thanks in advance rene

Hello Nicholas

Thanks for your answer.
But can you tell me how can i map the columns of my select statement
to the columns of the created table then? Of course i want to use
databinding and not manage all stuff by myself (thats what i did
before but i want to change that now).

I think your way is the right way :) also because i need to build up
some relations but i only want to load the data of of the related item
when the user clicks on some row in the grid. Therefore i must build
the relations allready when filling the "Master" DataTable. But this
only works if the Detail-Table is already defined in the DataSet..

I think there will be more questions coming :)
 
P

Peter Duniho

Nicholas said:
ReneMarxis,

If you want to display the columns in a particular order on the
DataGridView, then you are going to have to add the columns manually to
the DataGridView, and not let the grid automatically generate the
columns for you (you could also choose while manually adding the columns
to the grid which columns to display, and which not to).

This is not exactly true. You can programmatically adjust the display
order of columns in a DataGridView by changing the DisplayIndex property
of a given DataGridViewColumn instance.

This does, of course, require some reliable way of identifying each
column in the first place. But that would be a necessity in any case,
so I don't think that's a problematic limitation.

So, you can in fact populate the DataGridView automatically, and then
after that happens, go through each column and assign it a specific
DisplayIndex value. This will force each column into the desired position.

Note that since changing the DisplayIndex of one column has the effect
of the other columns renumbering themselves as necessary, it is best to
assign the new column DisplayIndex values starting at 0 and working
one's way right. That is, the order of the assignments should happen in
the order of the new column order, rather than the current one.

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

While this solution will work, it also requires operations on the order
of O(2N) (one for the auto-population, another to figure out what index to
display each column in, and which columns to show), whereas just iterating
through the columns once to add them is just O(N).
 
R

ReneMarxis

While this solution will work, it also requires operations on the order
of O(2N) (one for the auto-population, another to figure out what index to
display each column in, and which columns to show), whereas just iterating
through the columns once to add them is just O(N).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


This is not exactly true. You can programmatically adjust the display
order of columns in a DataGridView by changing the DisplayIndex property
of a given DataGridViewColumn instance.
This does, of course, require some reliable way of identifying each column
in the first place. But that would be a necessity in any case, so I don't
think that's a problematic limitation.
So, you can in fact populate the DataGridView automatically, and then
after that happens, go through each column and assign it a specific
DisplayIndex value. This will force each column into the desired
position.
Note that since changing the DisplayIndex of one column has the effect of
the other columns renumbering themselves as necessary, it is best to
assign the new column DisplayIndex values starting at 0 and working one's
way right. That is, the order of the assignments should happen in the
order of the new column order, rather than the current one.

Many thank. I ll try that tomorow in the morning. Atm i'm a little to
drunk to do that :) a girlfriend took out to some sea celeration ....
 
P

Peter Duniho

Nicholas said:
While this solution will work, it also requires operations on the
order of O(2N) (one for the auto-population, another to figure out what
index to display each column in, and which columns to show), whereas
just iterating through the columns once to add them is just O(N).

Um. So?

Since you brought the order of the algorithm into the picture: O(2N) is
for all intents and purposes the same as O(N). By definition. That's
basic CS stuff.

Claiming that one method is O(2N) while the other is O(N) is misleading,
because it implies that the time cost of the implementation would
actually double by using the DisplayIndex property. That implication is
false, given that the act of retrieving the data (the first N in your
"O(2N)") is generally far more expensive than the act of reordering the
columns (the second N in your "O(2N)").

If you want to measure absolute time differences between the two
methods, fine. Then do that (and please feel free to post the results
of your measurement of the relative difference between the two
implementations, as well as total time spent for each). But I see no
point in using the big O notation to contrast two things that are, in
the context of big O notation, exactly equivalent.

And if all that weren't enough to invalidate the point: if there are
enough columns that the order of the column-populating algorithm, never
mind a constant factor relating two otherwise identical orders, is in
any way relevant, that's a DataGridView that no user should be forced to
see.

In many situations it is far more convenient and maintainable to assign
order of columns by DisplayIndex rather than having to populate the
DataGridView manually. In fact, though I haven't looked at this code
specifically, I suspect the OP has exactly one of those situations.

No offense intended, but your stated concern is a classic example of not
just premature optimization, but _irrelevant_ optimization, and frankly
I think it sets a poor example for less-experienced programmers. IMHO,
you may want to rethink whether that's really a point you believe was
worth making.

Are you sure you didn't just want to reply "sorry, you're right...the OP
doesn't 'have to add the columns manually to the DataGridView' as I
originally wrote"? IMHO, that would have made a lot more sense. :)

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

Are you sure you didn't just want to reply "sorry, you're right...the OP
doesn't 'have to add the columns manually to the DataGridView' as I
originally wrote"? IMHO, that would have made a lot more sense. :)

No, this wasn't even close to my intent.
 
P

Peter Duniho

Nicholas said:
No, this wasn't even close to my intent.

Well, perhaps it should have been. Because, IMHO, what you did post was
not only not useful, it was terribly misleading to anyone actually
interested in performance issues.

The idea that one should avoid using the DisplayIndex as a way of
configuring the column order for the DataGridView because of a
performance issue is just plain wrong.

Pete
 
R

ReneMarxis

Well, perhaps it should have been. Because, IMHO, what you did post was
not only not useful, it was terribly misleading to anyone actually
interested in performance issues.

The idea that one should avoid using the DisplayIndex as a way of
configuring the column order for the DataGridView because of a
performance issue is just plain wrong.

Pete

Juast to let you know :) It works great with that DisplayOrder. Also
i'm not afraid of the performance as the DB is somewhere in the web
and so performance is not that fast by definition.

Now i got to work on the relations. I ll probably have some more
question to you.

Many thanks

rene
 
N

Nicholas Paldino [.NET/C# MVP]

Well, perhaps it should have been. Because, IMHO, what you did post was
not only not useful, it was terribly misleading to anyone actually
interested in performance issues.

The idea that one should avoid using the DisplayIndex as a way of
configuring the column order for the DataGridView because of a performance
issue is just plain wrong.

Fair enough, but to be quite honest, your overly condescending approach to
pointing this out leaves much to be desired. Your approach is insulting, to
say the least.
 
P

Peter Duniho

Nicholas said:
Fair enough, but to be quite honest, your overly condescending approach
to pointing this out leaves much to be desired. Your approach is
insulting, to say the least.

Ditto your original reply to me, stating that there was something wrong
with the DisplayIndex property when in fact you should have been
acknowledging your previous mistake (that is, telling the OP that he had
no choice but to add the columns manually one at a time), rather than
compounding it with another.

Frankly, I don't see what was so "overly condescending" about my reply
(as if there's any sort of condescending that isn't "overly"), but if my
reply was condescending, at least it had correct, factual information in
it (this time...I admit, I am not always right :) ).

If I hurt your feelings, I apologize. I'm simply trying to make sure
the facts here are correct. Unfortunately, in this case that required
contradicting what you posted (not something that normally happens,
obviously).

Pete
 
G

G.Doten

Peter said:
Ditto your original reply to me, stating that there was something wrong
with the DisplayIndex property when in fact you should have been
acknowledging your previous mistake (that is, telling the OP that he had
no choice but to add the columns manually one at a time), rather than
compounding it with another.

Frankly, I don't see what was so "overly condescending" about my reply
(as if there's any sort of condescending that isn't "overly"), but if my
reply was condescending, at least it had correct, factual information in
it (this time...I admit, I am not always right :) ).

If I hurt your feelings, I apologize. I'm simply trying to make sure
the facts here are correct. Unfortunately, in this case that required
contradicting what you posted (not something that normally happens,
obviously).

Pete

It's fine to contradict what someone has posted; it's the words and tone
a person chooses that makes all the difference. FWIW, it's why I'm
busting your b*lls in the serial number thread.

Of course, it doesn't help that this medium easily lends itself to
misunderstandings in intended (or not intended) tones in replies.
Emoticons just don't cut it to replace the face-to-face interaction of a
real conversation.

"Nobody asked, just my opinion."
 

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