I am using EditMode="InPlace" and AllowMultiRowEdit="True" to edit a RadGrid. I am also using a non RadGrid button to save the RadGrid to a database. Column1 in the RadGrid is read only. I am using ExtractValuesFromItem(newValues, editedItem) to get the cell that was changed. This works, but I don't know how to reference the value in the read only cell in column1 to see which row was edited. I need this to update the database based on the read only information in Column1. I will also be adding databound dropdown lists in the EditItem template so a solution that drastically changes the code below also needs to be able to handle that.
protected void SaveButton_Click(object sender, EventArgs e)
{
foreach (GridItem item in RadGrid1.Items)
{
if (item.IsInEditMode)
{
GridEditableItem editedItem = (GridEditableItem)item;
Hashtable newValues = new Hashtable();
editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
// I need to get the value in Column1 on the edited row.
string ReadOnlyCell = ["Column1"].toString();
string ChangedCell = newValues["Column2"].ToString();
// will update SQL here...
editedItem.Edit = false;
}
}
RadGrid1.Rebind();
}
7 Answers, 1 is accepted
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Nov 2012, 04:21 AM
Hello,
Thanks,
Jayesh Goyani
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"ID"
UniqueName
=
"ID"
HeaderText
=
"ID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Name"
UniqueName
=
"Name"
HeaderText
=
"Name"
>
</
telerik:GridBoundColumn
>
<
telerik:GridEditCommandColumn
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
if
(e.Item
is
GridEditFormInsertItem)
{
// insert
}
else
{
// Edit
// Please add below code in your page
GridEditableItem item = e.Item
as
GridEditableItem;
item[
"Name"
].Controls[0].Visible =
false
;
Label l1 =
new
Label();
l1.ID =
"l1"
;
l1.Text = (item[
"Name"
].Controls[0]
as
TextBox).Text;
item[
"Name"
].Controls.Add(l1);
}
}
}
protected
void
LinkButton1_Click(
object
sender, EventArgs e)
{
foreach
(GridItem item
in
RadGrid1.Items)
{
if
(item.IsInEditMode)
{
GridEditableItem editedItem = (GridEditableItem)item;
Hashtable newValues =
new
Hashtable();
editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
}
}
}
Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 12 Nov 2012, 05:43 AM
Hi Scott,
One suggestion is to set the ReadOnly Column as the DataKeyName of the gridtableview and access the value as shown below.
aspx:
C#:
Thanks,
Shinu.
One suggestion is to set the ReadOnly Column as the DataKeyName of the gridtableview and access the value as shown below.
aspx:
<
telerik:RadGrid
ID
=
"RadGrid1"
AllowMultiRowEdit
=
"true"
runat
=
"server"
>
<
MasterTableView
EditMode
=
"InPlace"
DataKeyNames
=
"OrderID"
>
protected
void
Button1_Click(
object
sender, EventArgs e)
{
foreach
(GridItem item
in
RadGrid1.EditItems)
{
GridEditableItem editedItem = (GridEditableItem)item;
string
value = editedItem.GetDataKeyValue(
"OrderID"
).ToString();
}
}
Thanks,
Shinu.
0
Scott
Top achievements
Rank 1
answered on 12 Nov 2012, 07:12 PM
Shinu,
Setting the ReadOnly Column as the DataKeyName works for the example I gave. It is good to know, but I actually have multiple read only columns that I need to access. Is there way read all items in the edited row? I tried the code below and get " " for the cellvalue on all three examples below.
Thanks,
Scott
Setting the ReadOnly Column as the DataKeyName works for the example I gave. It is good to know, but I actually have multiple read only columns that I need to access. Is there way read all items in the edited row? I tried the code below and get " " for the cellvalue on all three examples below.
GridEditableItem editedItem = (GridEditableItem)item;
int index = editedItem.ItemIndex;
string cellvalue1 = RadGrid1.Items[index]["OrderID"].Text;
string cellvalue2 = RadGrid1.Items[index]["CustomerID"].Text;
// another method
string cellvalue3 = RadGrid1.Items[index].Cells[0].Text;
Thanks,
Scott
0
Scott
Top achievements
Rank 1
answered on 12 Nov 2012, 07:27 PM
Jayesh,
Thanks for your reply. In your example you set the item to be not visible.
item["Name"].Controls[0].Visible = false;
When I set to be read only, newValues only contains the cell that was edited. If I remove the read only tag, then both values are shown.
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID" ReadOnly="True">
Thanks,
Scott
0
Scott
Top achievements
Rank 1
answered on 13 Nov 2012, 09:53 PM
Jayesh Goyani
I got it working by applying a label to the ItemTemplate (after turning the column into a template) and then using '(Label)item.FindControl("ColumnLabel")' to get the test of the label. I am not sure if that is what you had meant, but I have it working now.
Scott
I got it working by applying a label to the ItemTemplate (after turning the column into a template) and then using '(Label)item.FindControl("ColumnLabel")' to get the test of the label. I am not sure if that is what you had meant, but I have it working now.
Scott
0
Jayesh Goyani
Top achievements
Rank 2
answered on 14 Nov 2012, 07:00 PM
Hello,
Let me know which code you want to understand.
Thanks,
Jayesh Goyani
Let me know which code you want to understand.
Thanks,
Jayesh Goyani
0
Asad
Top achievements
Rank 1
answered on 06 Dec 2013, 08:33 AM
Hi Scott,
One suggestion is to set the ReadOnly Column as the DataKeyName of the gridtableview and access the value as shown below.
aspx:
C#:
Thanks Shinu,, been searching for this
One suggestion is to set the ReadOnly Column as the DataKeyName of the gridtableview and access the value as shown below.
aspx:
<
telerik:RadGrid
ID
=
"RadGrid1"
AllowMultiRowEdit
=
"true"
runat
=
"server"
>
<
MasterTableView
EditMode
=
"InPlace"
DataKeyNames
=
"OrderID"
>
protected
void
Button1_Click(
object
sender, EventArgs e)
{
foreach
(GridItem item
in
RadGrid1.EditItems)
{
GridEditableItem editedItem = (GridEditableItem)item;
string
value = editedItem.GetDataKeyValue(
"OrderID"
).ToString();
}
}
Thanks Shinu,, been searching for this