By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Field rendering in DataGridView with dynamic query

P: n/a
If I populate a DataGridView with a query supplied at runtime, boolean fields
render as a CheckBox instead of just a text rendering of '0' or '1'. That is
nice sometimes, but I would like the flexibility of showing text sometimes as
well. Is there an easy way to get the text '0' or '1' instead of the
CheckBox? My DataGridView is filled in with this basic code:

DataTable dataTable = new DataTable();
qBindingSource.DataSource = dataTable;
qBindingSource.DataMember = null;
SqlDataAdapter tableAdapter = new SqlDataAdapter(myQuery, myConnString);
tableAdapter.Fill(dataTable);

Note that the query is completely arbirtrary so I do not know in advance
which columns, if any, are boolean.

My basic approach would be to have a button that lets the user switch
between "raw" (i.e. 0 or 1 showing) and "enhanced" (i.e. with checkboxes)
displays. Then after I load the DataGridView, scan each column type, looking
for DataGridViewCheckBoxColumn. Could I just change that to
DataGridViewTextBoxColumn on the fly? Also, when I am showing checkboxes, I
want to distinguish 0 from null, so could I change the ThreeState property on
the fly as well?

Environment: C# 2.0, .NET 2.0, VS2005
Jan 31 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Michael,

That's exactly how I would do it. Instead of letting the DataGridView
figure out how to display the columns, you manually add the columns that you
want to it, applying whatever logic you want (in this case, the checkbox
with the three-state logic, or the text column) and then set the data
source.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"michael sorens" <m_********@newsgroup.nospamwrote in message
news:56**********************************@microsof t.com...
If I populate a DataGridView with a query supplied at runtime, boolean
fields
render as a CheckBox instead of just a text rendering of '0' or '1'. That
is
nice sometimes, but I would like the flexibility of showing text sometimes
as
well. Is there an easy way to get the text '0' or '1' instead of the
CheckBox? My DataGridView is filled in with this basic code:

DataTable dataTable = new DataTable();
qBindingSource.DataSource = dataTable;
qBindingSource.DataMember = null;
SqlDataAdapter tableAdapter = new SqlDataAdapter(myQuery, myConnString);
tableAdapter.Fill(dataTable);

Note that the query is completely arbirtrary so I do not know in advance
which columns, if any, are boolean.

My basic approach would be to have a button that lets the user switch
between "raw" (i.e. 0 or 1 showing) and "enhanced" (i.e. with checkboxes)
displays. Then after I load the DataGridView, scan each column type,
looking
for DataGridViewCheckBoxColumn. Could I just change that to
DataGridViewTextBoxColumn on the fly? Also, when I am showing checkboxes,
I
want to distinguish 0 from null, so could I change the ThreeState property
on
the fly as well?

Environment: C# 2.0, .NET 2.0, VS2005
Jan 31 '08 #2

P: n/a
While you said "yes" to my suggestion, your explanation implies "no". Let me
explain. My sample code populates the DataGridView "automatically". I wanted
to just change the column type (somehow) and have it then behave differently.
What I infer from your response, however, is that I need to replace the
column with one of a different type. I found a post that indicates that this
means all the data is lost, however
(http://forums.microsoft.com/MSDN/Sho...7676&SiteID=1). The
performance hit, I suspect, would be too great on a table with a hundred
thousand lines, for example.

So, backing up to your actual suggestion--creating the DataGridView
customized from the start--could you point me to any sample code that
populates the DataGridView less automatically so that I could, as you
suggest, customize to what I want?
Jan 31 '08 #3

P: n/a
That does just what I needed; thanks!
Feb 1 '08 #4

P: n/a
[I am hoping this will still get read even though I already marked that the
question had been answered...]

While my original question was answered, I have a follow-up:
Changing the CheckBox column to a Text column renders "True" or "False"
rather than a checked/unchecked box. That is better for my application, but
what I really want is the raw data, i.e. 0 or 1. How does one do this?
Feb 13 '08 #5

P: n/a
Hi Michael,
That is better for my application, but what I really want is the raw
data, i.e. 0 or 1. How does one do this?

You can handle the CellFormatting event of the DataGridView to show 0 or 1
instead of True or False in the cells.

The following is a sample:

void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv.Columns[e.ColumnIndex].GetType() ==
typeof(DataGridViewTextBoxColumn)
&& e.Value != null && e.Value.GetType() == typeof(bool))
{
if ((bool)e.Value)
e.Value = "1";
else
e.Value = "0";

e.FormattingApplied = true;
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Feb 14 '08 #6

P: n/a
Thanks for the pointer on that; right on target!

Last question on the same topic: For a column that contains a DateTime
value, I tracked down an issue where the column of a DataGridView renders a
date in a default format of "mm/dd/yyyy hh:mm AM" yet if I print the value
from that column to the console it has seconds as well. How do I change the
date formatting in a DataGridView to show the seconds as well? Or to show the
time without the date?

Feb 16 '08 #7

P: n/a
Hi Michael,
How do I change the date formatting in a DataGridView to show the seconds
as well? Or to show the time without the date?

You can set the Format property of the DefaultCellStyle property of the
DataGridView column that contains date time values to the "T". For example,

this.dataGridView1.DataSource = datatable;
for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
if (this.dataGridView1.Columns[i].ValueType == typeof(DateTime))
{
DataGridViewCellStyle dgvcellStyle = new
DataGridViewCellStyle();
dgvcellStyle.Format = "T";
this.dataGridView1.Columns[i].DefaultCellStyle = dgvcellStyle;
}
}

Hope this helps.
If you have any question, please feel free to let me know.

PS. (just a reminding) You'd better post new posts for new questions in the
future. Thanks!
Sincerely,
Linda Liu
Microsoft Online Community Support

Feb 18 '08 #8

P: n/a
DataGridViewCellStyle dgvcellStyle = new
DataGridViewCellStyle();
dgvcellStyle.Format = "T";
this.dataGridView1.Columns[i].DefaultCellStyle =
dgvcellStyle;
Much simpler is set DefaultCellStyle directly:

this.dataGridView1.Columns[i].DefaultCellStyle.Format = "T";

Andrus.
Feb 18 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.