Question adding date in datagrid

svibuk

Active member
Joined
Jul 15, 2008
Messages
31
Programming Experience
1-3
---------------------------
DataGridView Default Error Dialog
---------------------------
The following exception occurred in the DataGridView:



System.FormatException: String was not recognized as a valid DateTime. ---> System.FormatException: String was not recognized as a valid DateTime.

at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)

at System.DateTime.Parse(String s, IFormatProvider provider)

--- End of inner exception stack trace ---

at System.Windows.Forms.Formatter.InvokeStringParseMethod(Object value, Type targetType, IFormatProvider formatInfo)

at System.Windows.Forms.Formatter.ParseObjectInternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue)

at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)

at System.Windows.Forms.DataGridViewCell.ParseFormattedValueInternal(Type valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)

at System.Windows.Forms.DataGridViewCell.ParseFormattedValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)

at System.Windows.Forms.DataGridView.PushFormattedValue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)



To replace this default dialog please handle the DataError event.
---------------------------
OK
---------------------------
 
Were you thinking of asking a question? The dialogue you have copied the contents of tells you what the issue is and how to handle it so you have all the information you need. If you have more to add or ask then please do so and please be specific rather than doing the minimum and expecting us to divine the rest.
 
Were you thinking of asking a question? The dialogue you have copied the contents of tells you what the issue is and how to handle it so you have all the information you need. If you have more to add or ask then please do so and please be specific rather than doing the minimum and expecting us to divine the rest.


i copied the error msg which i get when i try 2 enter date in the grid cell
i need to add date in dd/mm/yyyy format in the grid cell
 
Then that's what you should have said in the first place. In .NET, dd/mm/yyyy would actually be day, minute year. If you want day, month year then that's dd/MM/yyyy. Go into the DefaultCellStyle property of your column and you can set the Format, which takes format specifiers as ToString and String.Format for formatting numbers, dates and times.
 
That said, if you're using a text box cell then the user can enter anything they like, so you'll still have to handle the situation where the entered value can't be converted to a valid Date.
 
That said, if you're using a text box cell then the user can enter anything they like, so you'll still have to handle the situation where the entered value can't be converted to a valid Date.


ya i had tried setting the cell format
my grid column 5 & 6 hve the date

but i hve not binded the grid thr' properties
i hve binded it thr' code
i have sql select * from table & binded the grid
so how do i set the column format at runtime?
 
The fact that you're binding the data to the grid at run time does not mean that you cannot configure the grid at design time. Currently you are relying on the grid to create the columns for you automatically but you can add the columns in the designer and set the DataPropertyName to specify which column of the data source to bind to. That will allow you to set the Format at design time too.

If you do want to do it at run time though, it's still the same property. Think about a TextBox. If you want to display text in it at design time then you set the Text property and if you want to display text in it at run time you set the Text property. It's exactly the same property regardless. The same goes here. You get the property, get its DefaultCellStyle property and set its Format property, exactly as you would at design time.
 
The fact that you're binding the data to the grid at run time does not mean that you cannot configure the grid at design time. Currently you are relying on the grid to create the columns for you automatically but you can add the columns in the designer and set the DataPropertyName to specify which column of the data source to bind to. That will allow you to set the Format at design time too.

If you do want to do it at run time though, it's still the same property. Think about a TextBox. If you want to display text in it at design time then you set the Text property and if you want to display text in it at run time you set the Text property. It's exactly the same property regardless. The same goes here. You get the property, get its DefaultCellStyle property and set its Format property, exactly as you would at design time.



as per u r suggestion i set the column data at design time
and binded at run time as DataGridView1.DataSource = ds1.Tables("trans")

but issue is i get double columns
ie first foure coulmns i get which i binded at design time but without data & next four columns frm the query but with data
 
if i set at run time i get error index out of range
grid_trans.Columns(1).DefaultCellStyle.Format = DateFormat.ShortDate
 
If you get that exception then that means there's no column at that index but why would you need to do it at run time anyway? Just read what I wrote in post #7 and do it.
 
i am really sorry but even on repeatedly reading post 7 i am nt able to make out the difference
the cell / column property can be assigned at design or runtime

if assigned at design time i already wrote that i get double columns
if assigned at runtime before binding to the grid i get index out of range error
if i assign it after binding to the grid then also i get error

otherthan this culd u plz explain
 
svibuk said:
but issue is i get double columns
ie first foure coulmns i get which i binded at design time but without data & next four columns frm the query but with data
To resolve:
post 7 said:
set the DataPropertyName to specify which column of the data source to bind to
 
thats what i had done but after doing it i was getting double columnname

currently wht i have fr binding the grid is

in form load i hve
sqlstr="
select p.id , p.pname ,0 as QTY, p.RATE, AMOUNT, p.dtfrm as From_DT,p.dtto as To_DT from PRDT left join M_customer C On C.Cid = t.cust_id left join M_prdt P On P.id = t.id where t.cust_id = " & dropdown1.SelectedValue & "
grid_trans.DataSource = ds1.Tables("trans")
grid_trans.AllowUserToAddRows = False
grid_trans.Columns(0).Visible = False


grid_trans.Columns(5).DefaultCellStyle.Format = DateFormat.ShortDate
grid_trans.Columns(6).DefaultCellStyle.Format = DateFormat.ShortDate



thios is how i bind the grid on the form load
from this tell me wht to do
 
Back
Top