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

Quick check to see if Columnname exists in a datagridview

RSH
Hi,

I have a situation where I have a dynamically loaded DataGridView and I need
to see if a particular column exists in the DataGridView before I enable it
for editing. Is there an easy way to see if the column exists?

Thanks!
Ron
Mar 20 '06 #1
7 32727
RSH
Silly me!

In case anyone else was wondering...

if (dataGridView1.Columns["Modified"] != null)

{

dataGridView1.Columns["Modified"].ReadOnly = true;

}

"RSH" <wa*************@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

I have a situation where I have a dynamically loaded DataGridView and I
need to see if a particular column exists in the DataGridView before I
enable it for editing. Is there an easy way to see if the column exists?

Thanks!
Ron

Mar 20 '06 #2
Please use the "Columns.Contains("column name")" method.
Example:

Lets say we have a DataGrid called "dataGridView1".

this.dataGridView1.Columns.Add("One", "One");

if (this.dataGridView1.Columns.Contains("One"))
System.Windows.Forms.MessageBox.Show("Yes");

this.dataGridView1.Columns.Add("Two", "Two");
this.dataGridView1.Columns.Add("Three", "Three");
Regards,
Lars-Inge Tønnessen
Siemens Medical - Norway
Mar 20 '06 #3
Please use "Contains". :o)

Regards,
Lars-Inge Tønnessen
Siemens Medical - Norway
Mar 20 '06 #4
RSH
What is the difference? This appears to work as is outright.
"Lars-Inge Tønnessen (VJ# MVP)" <http://emailme.larsinge.com> wrote in
message news:u$**************@tk2msftngp13.phx.gbl...
Please use the "Columns.Contains("column name")" method.
Example:

Lets say we have a DataGrid called "dataGridView1".

this.dataGridView1.Columns.Add("One", "One");

if (this.dataGridView1.Columns.Contains("One"))
System.Windows.Forms.MessageBox.Show("Yes");

this.dataGridView1.Columns.Add("Two", "Two");
this.dataGridView1.Columns.Add("Three", "Three");
Regards,
Lars-Inge Tønnessen
Siemens Medical - Norway

Mar 20 '06 #5
1. Its a more reliable method of determining if a column exists. By your
method what if you port your code to .Net 2.x and in the new version it does
not return null but instead it returns an empty column?

2. Its just better coding practice. It begs the question, why did Microsoft
bother giving you a Contains method? Think about it.

3. It also explicitly states your intentions, which is probably the most
important factor of all.

--
-Demetri
"RSH" wrote:
What is the difference? This appears to work as is outright.
"Lars-Inge Tønnessen (VJ# MVP)" <http://emailme.larsinge.com> wrote in
message news:u$**************@tk2msftngp13.phx.gbl...
Please use the "Columns.Contains("column name")" method.
Example:

Lets say we have a DataGrid called "dataGridView1".

this.dataGridView1.Columns.Add("One", "One");

if (this.dataGridView1.Columns.Contains("One"))
System.Windows.Forms.MessageBox.Show("Yes");

this.dataGridView1.Columns.Add("Two", "Two");
this.dataGridView1.Columns.Add("Three", "Three");
Regards,
Lars-Inge Tønnessen
Siemens Medical - Norway


Mar 20 '06 #6
The Microsoft developers always use this method when they are writing code.
I guess it has something to do with forward compatibility and stability.

Regards,
Lars-Inge Tønnessen
Mar 20 '06 #7
RSH
Fair enough.

Thank you.

Ron

"Lars-Inge Tønnessen (VJ# MVP)" <http://emailme.larsinge.com> wrote in
message news:Ol*************@TK2MSFTNGP10.phx.gbl...
The Microsoft developers always use this method when they are writing
code. I guess it has something to do with forward compatibility and
stability.

Regards,
Lars-Inge Tønnessen

Mar 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is...
1
by: Pete | last post by:
Hi, i'm building a datagrid that grabs a documents location out of a database and puts them in as a hyper link column. Ideally i'd love to be able to check the validity of the link and only...
1
by: James | last post by:
vb.net 2003 i wrote a windows service that does threading. My codes are a) the service thread will read a list of machine from a text file (machines.txt). b) it then logon using the below...
2
by: Kiran_Juikar | last post by:
How to add the header checkbox in the datagridview checkbox column? Is there any property available to add a header checkboxor one need to write the custom logic for this?
0
by: Herb | last post by:
is there a way in an SQL stored proc to determine if a temp table exists before selecting from it....I have a java app. that is calling a stored proc. i want to check if Java created the table before...
0
by: ramith | last post by:
Guys, I wrote a simple code to check the existence of a directory (in UNIX based file systems) which is working for me. Have I done it in the correct way? #include <dirent.h>
4
by: Mark Berry | last post by:
Hi, I'm working on my "last resort" error block for a web application. If the error occurs after a Request has been made, I want to show the URL. If the Request object is not available, I'll...
0
by: philip.poole | last post by:
Hi everyone, I am settings up some .NET controls on my website and want to create template controls to completely separate the business layer and the presentation layer. However I have...
2
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.