0

Hello all,
This is my first post here.Woohoo! Seems like an awesome place to collaborate. So heres my issue, thread title explains pretty well though. I have a C# app that I am working on, and I have a datagrid and one button in the UI. Heres me ISSUE: I have been trying to assign a C# string variable to a datetime value in my datagrid. Btw, in my datagrid, I have 4 columns, firstName, LastName, Membership, Date.

EXAMPLE: Member scans card. App pulls members info from the SQL database, populates datagrid, then compares current date with the last time his/her card was scanned(if applicable).(DONT NEED HELP ON COMPARING THE DATES, I KNOW HOW TO DO THAT)


Can someone please explain CLEARLY how to assign the Date columns value from my datagrid to a C# string variable?

And if you cant explain that, dull it down a little bit and explain how to simply assign a datagrid string value to a C# string variable. THANKS IN ADVANCE!

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Teme64
1

Here's how you get your row to variables:

string firstName;
string lastName;
bool membership;
DateTime cardDate;

// Add first row
dataGridView1.Rows.Add();

// Add some test data to 1st row
dataGridView1.Rows[0].Cells[0].Value = "John";
dataGridView1.Rows[0].Cells[1].Value = "Doe";
dataGridView1.Rows[0].Cells[2].Value = true;
dataGridView1.Rows[0].Cells[3].Value = DateTime.Now;

// Get data from 1st row
firstName = dataGridView1.Rows[0].Cells[0].Value.ToString();
lastName = dataGridView1.Rows[0].Cells[1].Value.ToString();
membership = (bool)dataGridView1.Rows[0].Cells[2].Value;
cardDate = (DateTime)dataGridView1.Rows[0].Cells[3].Value;

// Dump variables to check their values

Cell values are objects so you just have to cast them to proper type.

HTH

Votes + Comments
Teme64 is awesome. By far the best and most easy to understand explanation. If I could I would up your rep by 50! You just made my morning my friend. Keep rockin on mate!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.