This is a migrated thread and some comments may be shown as answers.

RadGrid row changed for GridTemplateColumn

5 Answers 174 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Avetik
Top achievements
Rank 1
Avetik asked on 12 Apr 2012, 05:36 AM
Hi, I have a radgrid with GridTemplateColumn whic has checkbox 2 textboxes and datepicker.
Is there a way to know if user has edited a value in one of those controls ? 
I need to know which row is updated and get old/new values.

Thanks 

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Apr 2012, 08:40 AM
Hi Avetik,

Please take a look into the following code I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"
onneeddatasource="RadGrid1_NeedDataSource"
onupdatecommand="RadGrid1_UpdateCommand" AutoGenerateColumns="false"
AutoGenerateEditColumn="true" onitemdatabound="RadGrid1_ItemDataBound">
    <MasterTableView DataKeyNames="EmployeeID">
        <Columns>
            <telerik:GridTemplateColumn>
                <EditItemTemplate>
                    <asp:CheckBox ID="Check" runat="server" Checked='<%#Eval("IsActive") %>' />
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("LastName") %>'></asp:TextBox>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:TextBox>
                    <telerik:RadDatePicker ID="RadDatePicker1" runat="server"></telerik:RadDatePicker>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString3"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
public SqlCommand SqlCommand = new SqlCommand();
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    string selectFilterQuery = "select top 5 * from [Employees]";
    SqlCommand.CommandText = selectFilterQuery;
    SqlDataAdapter adapter = new SqlDataAdapter(selectFilterQuery, conn);
    DataTable dt = new DataTable();
    conn.Open();
    adapter.Fill(dt);
    conn.Close();
    RadGrid1.DataSource = dt;
}
protected void RadGrid1_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
         
    GridEditableItem editItem = (GridEditableItem)e.Item;
    string DataKeyName = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["EmployeeID"].ToString();  // You will get the DataKeyName
    int rowindex = editItem.ItemIndex;  //Row index to identify the row edited
    String OldValue=(string)Session["savedOldValue"];  // you will get the Old Value
    TextBox LastName = (TextBox)editItem.FindControl("TextBox1");
    string newLastName = LastName.Text;  // new value
    TextBox EmployeeID = (TextBox)editItem.FindControl("TextBox2");
    string newEmployeeID = EmployeeID.Text.ToString();
    CheckBox Check = (CheckBox)editItem.FindControl("Check");
    string value = Check.Checked.ToString();
    string updateQuery = "Update Employees set EmployeeID='" + newEmployeeID + "',LastName='" + newLastName + "',IsActive='" + value + "'";
    conn.Open();
    SqlCommand.CommandText = updateQuery;
    SqlCommand.Connection = conn;
    SqlCommand.ExecuteNonQuery();
    conn.Close();
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        TextBox txtBox = e.Item.FindControl("TextBox1") as TextBox;
        Session["savedOldValue"] = txtBox.Text;
    }
}

Hope this helps.
Thanks,
Princy.
0
Avetik
Top achievements
Rank 1
answered on 12 Apr 2012, 04:48 PM
Hi Princy,

Thank you for the answer. Actually it will not help ... as I have the controls to be edited in place without opening edit panel.
Lets say I have ID and Name fields. ID is just a GridBoundColumn and the Name is defined like this : 

<
telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name"<br>                                    meta:resourcekey="NameResource" HeaderStyle-Width="100px">
<ItemTemplate>
<telerik:RadTextBox ID="Name" runat="server" DbValue='<%# Eval("Name") %>'>                                    </RadTextBox>
        </ItemTemplate>
</telerik:GridTemplateColumn>

So I want to get the ID if all rows for which Name was changed.

Thanks
0
Elliott
Top achievements
Rank 2
answered on 13 Apr 2012, 07:12 PM
try adding an OnTextChanged event handler to the RadTextBox
cast the sender to RadTextBox, then use NamingContainer as a GridDataItem

I did something similar only on the Edit Item
0
Princy
Top achievements
Rank 2
answered on 21 Apr 2012, 06:34 AM
Hi,

Try OnTextChanged event of RadTextBox as Marianne Seggerman suggested. Here is the code I tried, please take a look into the following code snippet.

ASPX:
<telerik:GridTemplateColumn HeaderText="Name" UniqueName="Name">
<ItemTemplate>
    <telerik:RadTextBox ID="Name" runat="server" Text='<%# Eval("ShipName") %>'
        AutoPostBack="true" ontextchanged="Name_TextChanged"></telerik:RadTextBox>
</ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void Name_TextChanged(object sender, EventArgs e)
{
    RadTextBox txtbox = (RadTextBox)sender;
    GridEditableItem item = (GridEditableItem)txtbox.NamingContainer;
    int keyValue = Convert.ToInt32(item.GetDataKeyValue("OrderID").ToString());
}

Thanks,
Princy.
0
Avetik
Top achievements
Rank 1
answered on 30 May 2012, 07:51 PM
Thank you for feedback. I found workaround :) with having some kind of a observable data structure.
Tags
Grid
Asked by
Avetik
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Avetik
Top achievements
Rank 1
Elliott
Top achievements
Rank 2
Share this question
or