Dim rowInfo As GridViewRowInfo = radGridInventoryRuns.Rows.AddNew()
rowInfo.Cells("RunYear").Value = Year(Now())
rowInfo.Cells("DateCreated").Value = Now()
Is there anyway to mimic the behaviour of the button at the bottom of the gridview without actually clicking it. I am using VS 2012 (vb.net) and the Q1 2014 version of the Telerik controls.
11 Answers, 1 is accepted
Thank you for writing.
The DefaultValuesNeeded event fires when the user enters the row for new records only, so that it can be populated with default values. In order to achieve similar functionality for the data rows as well, not only for the new rows, it is appropriate to use the CellEditorInitialized event and initialize the editor's value with the desired default value:
//initialize default values for the new rowprivate void radGridView1_DefaultValuesNeeded(object sender, GridViewRowEventArgs e){ e.Row.Cells["ContactName"].Value = "Default name"; e.Row.Cells["CustomerID"].Value = Guid.NewGuid().ToString().Substring(0,5); e.Row.Cells["CompanyName"].Value = Guid.NewGuid().ToString().Substring(0,5);}//initialize default values for the data rowprivate void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e){ if (e.Row is GridViewDataRowInfo) { if (e.Column.Name=="ContactName") { e.ActiveEditor.Value = "Default Value"; } }}I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
Private Sub cmdCreateNewInventoryRun_Click(sender As Object, e As EventArgs) Handles cmdCreateNewInventoryRun.Click
radGridInventoryRuns.Rows.AddNew()
End Sub
'Capture this event to insert default values into the new InventoryRun row - if you call addnew in code it does not fire the DefaultValuesNeeded event
Private Sub radGridInventoryRuns_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs) Handles radGridInventoryRuns.CellEditorInitialized
If TypeOf e.Row Is GridViewDataRowInfo Then
Select e.Column.Name
Case "DateCreated"
e.ActiveEditor.Value = Now()
Case "RunYear"
e.ActiveEditor.Value = Year(Now())
Case "InventoryDownloaded"
e.ActiveEditor.Value = False
End Select
End If
End Sub
Thank you for writing back.
The DefaultValuesNeeded event is appropriate to fill the default values for the new row and these default values will be stored in the new row. This event is purposed to handle exactly this case.
As to the data rows, when the editor is initialized, its default value is the current cell's value and it is possible to change it, but in order to store its value in the cell (when no other changes are performed), you should use the following approach. You can handle the new row with it as well:
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e){ if (e.Column.Name == "ContactName") { e.ActiveEditor.Value = "Default Value"; } else if (e.Column.Name == "CustomerID" || e.Column.Name == "CompanyName") { e.ActiveEditor.Value = Guid.NewGuid().ToString().Substring(0, 5); }}private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e){ if (currentValue != string.Empty && e.ActiveEditor == null && currentValue != radGridView1.CurrentCell.Value ) { radGridView1.CurrentCell.Value = currentValue; }}string currentValue = string.Empty;private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e){ if (e.ActiveEditor != null && e.ActiveEditor.Value != null) { currentValue = e.ActiveEditor.Value.ToString(); }}Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it on a way, which suit your requirement best.
Please do not hesitate to contact us if you have any additional questions.
Regards,
Desislava
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
This is not the experience that my end users want to have. I would like to have the user click a button on the form to add a new row and see the new values pre-populated in that new row after they click the button. Some of the columns in my gridview are Read Only to the user and that's why I want them pre-populated with default or calculated values. It works like this if you allow the user to click the "Add New Row" at the bottom of the gridview and use the defaultvaluesneeded event - I just don't understand why you can't call that same procedure but from a button outside of the gridview?
private void cmdCreateNewInventoryRun_Click(object sender, EventArgs e){ radGridInvRuns.Rows.AddNew();}//Capture this event to insert default values into the new InventoryRun row - if you call addnew in code it does not fire the DefaultValuesNeeded event private void radGridInventoryRuns_CellEditorInitialized(object sender, GridViewCellEventArgs e){ if (e.Row is GridViewDataRowInfo) { switch (e.Column.Name) { case "DateCreated": e.ActiveEditor.Value = DateAndTime.Now(); break; case "RunYear": e.ActiveEditor.Value = DateAndTime.Year(DateAndTime.Now()); break; case "InventoryDownloaded": e.ActiveEditor.Value = 0; break; case "RunClosed": e.ActiveEditor.Value = 0; break; } }}private string currentValue = string.Empty;private void RadGridInventory_CellEndEdit(object sender, GridViewCellEventArgs e){ if (currentValue != string.Empty && e.ActiveEditor == null && currentValue != radGridInvRuns.CurrentCell.Value) { radGridInvRuns.CurrentCell.Value = currentValue; }}private void RadGridInventory_CellValidating(object sender, CellValidatingEventArgs e){ if (e.ActiveEditor != null && e.ActiveEditor.Value != null) { currentValue = e.ActiveEditor.Value.ToString(); }}Thank you for writing back.
I have reviewed the whole thread and If I understand correctly you want to add new row with default values inserted to it upon a button click, not using the default new row behaviour.
Another approach would be to use the Add method of the Rows collection. For the columns that you do not want to set default value you can set the value to Nothing:
Private Sub radButton1_Click(sender As Object, e As EventArgs) radGridView1.Rows.Add(Nothing, "Default Value1", "Default Value2", Nothing)End SubAnother approach is to use the AddNew method and then change the values in the CollectionChanged event of the Rows collection:
Private Sub Rows_CollectionChanged(sender As Object, e As Telerik.WinControls.Data.NotifyCollectionChangedEventArgs) If e.Action = NotifyCollectionChangedAction.Add Then Dim newRow As GridViewDataRowInfo = TryCast(e.NewItems(0), GridViewDataRowInfo) newRow.Cells(1).Value = "Default Value 1" newRow.Cells(2).Value = "Default Value 2" End IfEnd SubMore information about this event can be found in the following article: Tracking changes in RadGridView.
I hope this helps.
Regards,
Dimitar
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
I am not sure why the event is not available on your side. This is why I want to kindly ask you to open a support ticket and provide a detailed information about your case.
I am looking forward to your reply.
Regards,
Dimitar
Progress Telerik
I have similiar problem like that
Private Sub TxtKodeBarang_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtKodeBarang.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Enter Then
Using db As New MTSEntities
Dim StrCari = db.MstBarangs.Where(Function(d) d.KodeBarang = TxtKodeBarang.Text).SingleOrDefault 'TxtKode.Text).FirstOrDefault
If IsNothing(StrCari) Then
BtnCrBarang_Click(sender, e)
Else
TxtKodeGudang.Text = StrCari.KodeBarang.ToString
LblNamaGudang.Text = StrCari.NamaBarang.ToString
End If
End Using
End If
End Sub
I need this code on radgrid_cell value changed.... how make this happen ? but not for fill the textbox but fill column1 and column2
In the CellValueChanged you can directly access the values of the current row:
Private Sub RadGridView1_CellValueChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) If e.Column.Name = "column4" Then e.Row.Cells("column1").Value = "Test" e.Row.Cells("column2").Value = "Test" End IfEnd SubI hope this will be useful.
Regards,
Dimitar
Progress Telerik

