10 Answers, 1 is accepted

Thanks,
Jayesh Goyani
<
MasterTableView
DataKeyNames
=
"ID"
>
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = e.Item
as
GridDataItem;
string
strID = item.GetDataKeyValue(
"ID"
).ToString();
}
}

GridDataItem item = e.Item
as
GridDataItem;
string
strID = item.GetDataKeyValue(
"ID"
).ToString();
if i use this code i am getting NULL Value into item object......
please help me.

Please try another approach which is shown below.
ASPX:
<
MasterTableView
DataKeyNames
=
"ID"
>
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
string
Id = item.OwnerTableView.DataKeyValues[item.ItemIndex][
"ID"
].ToString();
}
}
Thanks,
Shinu.

But here also i am getting item as NULL Value only........
Please help me
Thanks Inadvance...

Unfortunately i couldn't replicate the issue. Here is the sample code I tried to get the DataKeyValue in ItemDataBound event.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
DataSourceID
=
"SqlDataSource1"
AutoGenerateColumns
=
"false"
runat
=
"server"
onitemdatabound
=
"RadGrid1_ItemDataBound"
>
<
MasterTableView
DataKeyNames
=
"OrderID"
>
<
Columns
>
<
telerik:GridBoundColumn
UniqueName
=
"ShipName"
DataField
=
"ShipName"
></
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"ShipCity"
DataField
=
"ShipCity"
></
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"OrderID"
DataField
=
"OrderID"
></
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
string
strID = item.GetDataKeyValue(
"OrderID"
).ToString();
//OR
string
Id = item.OwnerTableView.DataKeyValues[item.ItemIndex][
"OrderID"
].ToString();
}
}
Please provide the code if it doesn't help.
Thanks,
Shinu.

Thank u for giving code.Now i want datakey value inside radbutton_Click() event.I am using code like as follows:
protected void radbutton_Click(object sender, EventArgs e)
{
RadButton BadOrder = (RadButton)sender;
GridDataItem item = BadOrder.NamingContainer as GridDataItem;
string id = Convert.ToString(item.GetDataKeyValue("OrderDetailID"));
}
Then i am getting ID=Null.....
So please help me..........

Please try with below code snippet/Demo.
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
>
<
MasterTableView
CommandItemDisplay
=
"Top"
DataKeyNames
=
"ID"
>
<
Columns
>
<
telerik:GridTemplateColumn
>
<
ItemTemplate
>
<
telerik:RadButton
ID
=
"RadButton1"
runat
=
"server"
Text
=
"RadButton1"
OnClick
=
"RadButton1_Click"
>
</
telerik:RadButton
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadButton
ID
=
"RadButton2"
runat
=
"server"
Text
=
"RadButton2"
OnClick
=
"RadButton2_Click"
>
</
telerik:RadButton
>
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridEditCommandColumn
></
telerik:GridEditCommandColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
GridDataItem item = (sender
as
RadButton).NamingContainer
as
GridDataItem;
string
strId = item.GetDataKeyValue(
"ID"
).ToString();
}
protected
void
RadButton2_Click(
object
sender, EventArgs e)
{
GridEditableItem item = (sender
as
RadButton).NamingContainer
as
GridEditableItem;
string
strId = item.GetDataKeyValue(
"ID"
).ToString();
}
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
dynamic data =
new
[] {
new
{ ID = 1, Name =
"aaa"
},
new
{ ID = 2, Name =
"bbb"
},
new
{ ID = 3, Name =
"ccc"
},
new
{ ID = 4, Name =
"ddd"
},
new
{ ID = 5, Name =
"eee"
},
new
{ ID = 6, Name =
"aaa"
},
new
{ ID = 7, Name =
"bbb"
},
new
{ ID = 8, Name =
"ccc"
},
new
{ ID = 9, Name =
"ddd"
},
new
{ ID = 10, Name =
"eee"
}
};
RadGrid1.DataSource = data;
}
Thanks,
Jayesh Goyani


in radgrid Itemcommand Event i am getting GetDataKeyValue and i am storing it in session after doing some operations like update and delete for some other divisions i am rebinding this grid.after doing rebind also i need that selected row only for that in itemdatabound i write the code like if session is equal to null then only it will go to that code are else it will select by defalut 1st row only.if session not equal to null then it will check
GridDataItem item = e.Item as GridDataItem;
if(item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString()==Session["id"])
item.SelecteValue=true;
so it will stay in perticular row only but in item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString() i am getting 1st row value if i select 2nd row also. in itemcommand i am getting 2nd row id and i am storing it in session but it is not comparing to itemdatabound value how to set selected row only afeter doing rebind also in itemdatabound

Hi,
Can someone please help me on how to Radgrid2 Data key Values in Radgrid1? Radgrid2 has a detail table. When I try the code below I get error saying
'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
Protected Sub Radgrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
If (TypeOf e.Item Is GridDataItem) Then
Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
Dim strSidPartNum As String = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("SID_PART").ToString
Dim strLoadPartNum As String = RadGrid2.MasterTableView.DataKeyValues("LOAD_PART").ToString
Dim strTotalQty As String = RadGrid2.MasterTableView.DataKeyValues(e.Item.ItemIndex)("LOAD_PART_SUM").ToString
Dim strQtyExp As String = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("QTY_EXP").ToString
'FORMATTING to a different color to highlight the part num and quantity variation between the SID and LOADS side
If strSidPartNum = strLoadPartNum Then
If strTotalQty <> strQtyExp Then
dataItem.ForeColor = Color.Red
Else
End If
End If
End If
Thank you.