473,404 Members | 2,195 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,404 software developers and data experts.

Field rendering in DataGridView with dynamic query

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
8 9017
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
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
That does just what I needed; thanks!
Feb 1 '08 #4
[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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
3
by: Peter Bailey | last post by:
Could someone please tell me how to pass criteria as if it were a parameter. I have a routine now that creates the sql string (well almost). at present the parameter is so I can pass one item ie...
1
by: Giloosh | last post by:
Hello i have been trying to figure out this problem for a very long time now, and thought maybe its worth asking for some help. I want to figure out if there is a way to create a field in a...
3
by: David Whitney | last post by:
All: I have a control that renders a table. As the table is rendered, each row in the table is constructed by creating a run-time (dynamic) object that is derived from an HtmlTableRow. The row...
10
by: Crazy Cat | last post by:
Hi, I am dynamically creating datagridviews for my application, then filling them with data from a 30 row datatable, and everything seems to work great with one exception. My horizontal...
5
by: devx777 | last post by:
Hello, I am trying to find some information or an example on how to build a dynamic query in DB2 that would allow me to join a table which its name is stored as a field value on another table....
2
by: Rich | last post by:
Hello, Some database applicatins have a tooltip feature where when you are dragging the scrollbar of the table view a tooltip appears next to the mouse cursor displaying the approximate record...
3
by: brian.turner | last post by:
This is sort of a design/javascript question. It involves CSS rendering in dynamic areas of a page using IE. E 6 & 7 HI There, We've created some example static html pages (using CSS) that...
0
by: frostbb | last post by:
Ok, stumped one more time, I'm trying to learn how to use a DataGridView in place of the old DataGrid control. QUESTION: How do I map the columns returned from a RunTime sql query to the columns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.