Hi,
I am trying to use the RadGrid control to implement a table
that has a field indicating if the record is a favourite,
based upon a database bit field. In the RadGrid the field is
an image button.
I am using the RadGrid_ItemCommand event
to toggle the status of the flag in the database for the record
that the user clicked on.
I use the item.GetDataKeyValue("<DataKeyField>") to get the IDs that are
used to identify the record to toggle.
The problem:
The value returned by item.GetDataKeyValue("cli_id") is not
always correct. It is sometimes a value corresponding to a
different row.
I also tried using the CommandArgument property, but that has the
same behavior.
How do I get the correct value for the row that was clicked on
I am trying to use the RadGrid control to implement a table
that has a field indicating if the record is a favourite,
based upon a database bit field. In the RadGrid the field is
an image button.
I am using the RadGrid_ItemCommand event
to toggle the status of the flag in the database for the record
that the user clicked on.
I use the item.GetDataKeyValue("<DataKeyField>") to get the IDs that are
used to identify the record to toggle.
The problem:
The value returned by item.GetDataKeyValue("cli_id") is not
always correct. It is sometimes a value corresponding to a
different row.
I also tried using the CommandArgument property, but that has the
same behavior.
How do I get the correct value for the row that was clicked on
by the user?
Here is the RadGrid_ItemCommand event:
protected
void
RadGrid1_ItemCommand(
object
source, GridCommandEventArgs e)
{
Trace.Write(
"RadGrid1_ItemCommand: starting up..."
);
if
(e.CommandName ==
"ChangeStatus"
)
{
GridDataItem item = (GridDataItem)e.Item;
//Get the record IDs from the DataKeyNames
System.Data.DataRowView oRow = (System.Data.DataRowView)item.DataItem;
int
iApp_id = Convert.ToInt32(item.GetDataKeyValue(
"app_id"
));
int
iCli_id = Convert.ToInt32(item.GetDataKeyValue(
"cli_id"
));
bool
bIsFav = Convert.ToBoolean(item.GetDataKeyValue(
"IsFav"
));
Trace.Write(
"RadGrid1_ItemCommand: iApp_id: "
+ iApp_id);
Trace.Write(
"RadGrid1_ItemCommand: cli_id: "
+ iCli_id);
Trace.Write(
"RadGrid1_ItemCommand: bIsFav: "
+ bIsFav);
int
itmp = Convert.ToInt32(e.CommandArgument);
//Toggle the Favorite status for that combination
SqlConnection oConn =
new
SqlConnection(SqlDataSource_GridViewSolutions.ConnectionString);
SqlCommand oCmd =
new
SqlCommand(
"toggle_gru_IsFav_Status"
, oConn);
oCmd.CommandType = System.Data.CommandType.StoredProcedure;
//ToDo make User name dynamic: oCmd.Parameters.Add(this.Page.User.Identity.Name);
oCmd.Parameters.Add(
new
SqlParameter(
"strLoginID"
,
"the_user"
));
oCmd.Parameters.Add(
new
SqlParameter(
"app_id"
, iApp_id));
oCmd.Parameters.Add(
new
SqlParameter(
"cli_id"
, iCli_id));
try
{
oConn.Open();
oCmd.ExecuteNonQuery();
}
catch
(Exception ex1)
{
Response.Write(
"Error updating favorite status: "
+ ex1.Message);
}
finally
{
oConn.Close();
oConn.Dispose();
}
RadGrid1.Rebind();
}
Here is the markup for the RadGrid control:
<
div
class
=
"apps_by_clients"
>
<
asp:SqlDataSource
ID
=
"SqlDataSource_GridViewSolutions"
ConnectionString
=
"Data Source=BTSLSQLDEV01;Initial Catalog=GenericStorage;User ID=userid;pwd=xxxxxxxxx"
SelectCommand
=
"EXEC GetAppsWithFavStatus 'the_user'"
runat
=
"server"
/>
</
div
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AllowPaging
=
"True"
CellSpacing
=
"0"
DataSourceID
=
"SqlDataSource_GridViewSolutions"
GridLines
=
"None"
PageSize
=
"30"
Skin
=
"Vista"
Width
=
"720px"
AutoGenerateColumns
=
"False"
AllowFilteringByColumn
=
"True"
GroupingSettings-CaseSensitive
=
"false"
onitemcommand
=
"RadGrid1_ItemCommand"
OnPreRender
=
"RadGrid1_PreRender"
>
<
MasterTableView
DataSourceID
=
"SqlDataSource_GridViewSolutions"
GroupsDefaultExpanded
=
"false"
DataKeyNames
=
"app_id, cli_id, IsFav"
>
<
CommandItemSettings
ExportToPdfText
=
"Export to PDF"
></
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
></
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
></
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"cli_name"
FilterControlAltText
=
"Filter cli_name column"
HeaderText
=
"cli_name"
UniqueName
=
"cli_name"
SortExpression
=
"cli_name"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"app_name"
FilterControlAltText
=
"Filter app_name column"
HeaderText
=
"app_name"
SortExpression
=
"app_name"
UniqueName
=
"app_name"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"app_id"
DataType
=
"System.Decimal"
FilterControlAltText
=
"Filter app_id column"
HeaderText
=
"app_id"
ReadOnly
=
"True"
SortExpression
=
"app_id"
UniqueName
=
"app_id"
>
</
telerik:GridBoundColumn
>
<
telerik:GridTemplateColumn
DataField
=
"IsFav"
HeaderText
=
"Favourite?"
UniqueName
=
"IsFav"
AllowFiltering
=
"false"
>
<
ItemTemplate
>
<
asp:ImageButton
ID
=
"ImageButton1"
runat
=
"server"
ImageUrl
=
'./'
CommandName
=
"ChangeStatus"
CommandArgument='<%# Eval("cli_id") %>'
/>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
></
EditColumn
>
</
EditFormSettings
>
<
PagerStyle
PageButtonCount
=
"5"
/>
</
MasterTableView
>
<
PagerStyle
PageButtonCount
=
"5"
/>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
We are using ASP.Net 4.0 on Windows server 2008
We are using 2012.2.607.40 of the Telerik Radcontrols
Cheers,
Geoff