Drop the time in Date field.

G

Guest

This is for a Win form.

SP sends back the PostMark date to my datagrid, shows proper date in
datagrid. When user clicks on datagrid, the code sends the current row to
text box. The text box shows the date and 12:00:00 AM. How do I get rid of
the time?

My code below.

int rowNum = dgDealerInfo.CurrentCell.RowNumber;
object Cell1 = dgDealerInfo[rowNum, 0];
object Cell2 = dgDealerInfo[rowNum, 1];
object Cell3 = dgDealerInfo[rowNum, 2];
object Cell4 = dgDealerInfo[rowNum, 3];
object Cell5 = dgDealerInfo[rowNum, 4];
string s = string.Format(Cell3 + System.Environment.NewLine + Cell4 +
System.Environment.NewLine + Cell5);
string s1 = string.Format(Cell1 + "");
string s2 = string.Format(Cell2 + "");
 
G

Guest

I get an error. "Cannot implicitly convert type 'object' to 'System.DateTime'.

Here is my code.

DateTime Cell3 = dgDealerInfo[rowNum, 2];
ResultForm.SetTextBox(Cell3.ToShortDateString());


dgDealerInfo[rowNum, 2] is a cell in my datagrid on my winform.
ResultForm.SetTextBox is a method on my winform


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Use DateTime.ToShortDateFormat();

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Mike L said:
This is for a Win form.

SP sends back the PostMark date to my datagrid, shows proper date in
datagrid. When user clicks on datagrid, the code sends the current row to
text box. The text box shows the date and 12:00:00 AM. How do I get rid
of
the time?

My code below.

int rowNum = dgDealerInfo.CurrentCell.RowNumber;
object Cell1 = dgDealerInfo[rowNum, 0];
object Cell2 = dgDealerInfo[rowNum, 1];
object Cell3 = dgDealerInfo[rowNum, 2];
object Cell4 = dgDealerInfo[rowNum, 3];
object Cell5 = dgDealerInfo[rowNum, 4];
string s = string.Format(Cell3 + System.Environment.NewLine + Cell4 +
System.Environment.NewLine + Cell5);
string s1 = string.Format(Cell1 + "");
string s2 = string.Format(Cell2 + "");
 
C

carion1

You could also use:

DateTime myDate = DateTime.Now();
string.Format("{0:d}", myDate);
 
J

Jon Skeet [C# MVP]

Mike L said:
I get an error. "Cannot implicitly convert type 'object' to 'System.DateTime'.

So cast the expression to a DateTime before trying to use it as one.

(Looking back to your original code, using x+"" to convert x to a
string is pretty nasty - use x.ToString(), or Convert.ToString(x).)
 
G

Guest

Thank you.

I cleaned up my code.

int rowNum = dgDealerInfo.CurrentCell.RowNumber;
string Cell1 = dgDealerInfo[rowNum, 0].ToString();
string Cell2 = dgDealerInfo[rowNum, 1].ToString();
string Cell3 = dgDealerInfo[rowNum, 2].ToString();
DateTime Cell4 = (DateTime) dgDealerInfo[rowNum, 3];
string Cell5 = dgDealerInfo[rowNum, 4].ToString();
string s = string.Format(Cell3 + System.Environment.NewLine +
string.Format("{0:d}", Cell4) + System.Environment.NewLine + Cell5);
ResultForm.SetTextBox(s);
 
J

Jon Skeet [C# MVP]

Mike L said:
Thank you.

I cleaned up my code.

int rowNum = dgDealerInfo.CurrentCell.RowNumber;
string Cell1 = dgDealerInfo[rowNum, 0].ToString();
string Cell2 = dgDealerInfo[rowNum, 1].ToString();
string Cell3 = dgDealerInfo[rowNum, 2].ToString();
DateTime Cell4 = (DateTime) dgDealerInfo[rowNum, 3];
string Cell5 = dgDealerInfo[rowNum, 4].ToString();
string s = string.Format(Cell3 + System.Environment.NewLine +
string.Format("{0:d}", Cell4) + System.Environment.NewLine + Cell5);
ResultForm.SetTextBox(s);

Why are you calling string.Format twice? What are you trying to
actually *format* by the time you've concatenated the various strings
together?

(You might want to consider more meaningful names than Cell1, Cell2,
Cell3 etc too.)
 
G

Guest

I WAS calling string.Format twice because I'm still learning .Net programming.

I was using Cell name variables because I was still in draft stage. Now
that I know for sure what data I'm pulling through, I modified the variables.

int rowNum = dgDealerInfo.CurrentCell.RowNumber;
string sFEI = dgDealerInfo[rowNum, 0].ToString();
string sSite = dgDealerInfo[rowNum, 1].ToString();
string sName = dgDealerInfo[rowNum, 2].ToString();
DateTime dtPostMark = (DateTime)
dgDealerInfo[rowNum, 3];
string sAmount = dgDealerInfo[rowNum, 4].ToString();
string s = sName + System.Environment.NewLine +
string.Format("{0:d}", dtPostMark) + System.Environment.NewLine + sAmount;
ResultForm.SetTextBox(s);
ResultForm.SetDealerFEI(Cell1);
ResultForm.SetDealerSite(Cell2);
this.DialogResult = DialogResult.OK;


Jon Skeet said:
Mike L said:
Thank you.

I cleaned up my code.

int rowNum = dgDealerInfo.CurrentCell.RowNumber;
string Cell1 = dgDealerInfo[rowNum, 0].ToString();
string Cell2 = dgDealerInfo[rowNum, 1].ToString();
string Cell3 = dgDealerInfo[rowNum, 2].ToString();
DateTime Cell4 = (DateTime) dgDealerInfo[rowNum, 3];
string Cell5 = dgDealerInfo[rowNum, 4].ToString();
string s = string.Format(Cell3 + System.Environment.NewLine +
string.Format("{0:d}", Cell4) + System.Environment.NewLine + Cell5);
ResultForm.SetTextBox(s);

Why are you calling string.Format twice? What are you trying to
actually *format* by the time you've concatenated the various strings
together?

(You might want to consider more meaningful names than Cell1, Cell2,
Cell3 etc too.)
 

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