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

Automatic Edit and Insert problem after external filtering

1 Answer 45 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 24 Oct 2010, 04:16 AM
Hi guys
I have enabled Automatic edit delete and insert in radgrid with sql data source. I have an external filter (radcombo) for filtering radgrid with distinct values of first three columns. Everything is working properly. But my issue is after filtering edit is not working properly. When i click on edit image button on a row in filtered grid i am getting first row in that grid instead of clicked item. I think, when i click on edit button radgrid going back to previous data source. How can I edit that particular clicked row in that grid. Here is my code. Please help anybody to clear this issue.

Ben

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGridOpenItems">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGridOpenItems" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="RadGridOpenItems">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGridOpenItems" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
              
            </AjaxSettings>
        </telerik:RadAjaxManager>
<telerik:RadComboBox ID="RadComboBox1" runat="server"
                MarkFirstMatch="True" EnableLoadOnDemand="True" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
 <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">

            <script type="text/javascript">
                function RowDblClick(sender, eventArgs) {
                    sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
                }
            </script>

        </telerik:RadCodeBlock>

<telerik:RadGrid ID="RadGridOpenItems" runat="server" DataSourceID="SqlDataSourcePwOpenItems" AllowMultiRowEdit="true"
         ShowStatusBar="True" GridLines="None" OnItemUpdated="RadGrid1_ItemUpdated" OnItemInserted="RadGrid1_ItemInserted"
        AllowFilteringByColumn="false" AllowPaging="True" OnItemDeleted="RadGrid1_ItemDeleted"  
    AllowSorting="True" Skin="Vista" AllowAutomaticDeletes="True"  
        AllowAutomaticInserts="True" AllowAutomaticUpdates="True"
        AutoGenerateColumns="False" ShowGroupPanel="false" >
        <ClientSettings Scrolling-AllowScroll="True" AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                <Selecting AllowRowSelect="True" />
                <Scrolling AllowScroll="True" UseStaticHeaders="True" FrozenColumnsCount="0" SaveScrollPosition="True" />
                <Resizing AllowColumnResize="True" />
                <ClientEvents OnRowDblClick="RowDblClick" />

                <Selecting AllowRowSelect="True"></Selecting>
                <Scrolling AllowScroll="True" UseStaticHeaders="True"></Scrolling>
                <Resizing AllowColumnResize="True"></Resizing>
        </ClientSettings>
        <MasterTableView DataSourceID="SqlDataSourcePwOpenItems"
            AllowPaging="True" AllowSorting="True" CommandItemDisplay="Top"
             EditMode="InPlace" DataKeyNames="PWOPENITEMS_ID" AllowAutomaticDeletes="true">
                <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
 
 
        <RowIndicatorColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>

        <ExpandCollapseColumn>
            <HeaderStyle Width="20px"></HeaderStyle>

        </ExpandCollapseColumn>
      
       
<%--Gridview Columns--%>   
    <Columns>
        <telerik:GridEditCommandColumn UniqueName="EditCommandColumn"
            ButtonType="imagebutton" >
            <HeaderStyle Width="35px" />
        </telerik:GridEditCommandColumn>

        <telerik:GridClientDeleteColumn ConfirmText="Are you sure you want to delete the selected row?"
              HeaderStyle-Width="35px" ButtonType="ImageButton" UniqueName="column1" >

        <HeaderStyle Width="35px"></HeaderStyle>
          </telerik:GridClientDeleteColumn>

        <telerik:GridTemplateColumn DataField="OPCODE" HeaderText="OPCODE"
            SortExpression="OPCODE" UniqueName="OPCODE" AllowFiltering="false">
            <HeaderStyle Width="120px" />
<%--Convert to uppercase--%>        
            <EditItemTemplate>
                <asp:TextBox id="txtOpcode" runat="server" Text='<%# Bind("OPCODE") %>' MaxLength="3"
                    Onkeyup="javascript:this.value = this.value.toUpperCase();"></asp:TextBox>
            </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label id="lblOpcode" runat="server" Text='<%# Eval("OPCODE") %>'></asp:Label>
                </ItemTemplate>
        </telerik:GridTemplateColumn>
 .
 .column2, column3,...
 .

 </Columns>

        <EditFormSettings FormCaptionStyle-HorizontalAlign="Center">
       
            <EditColumn UniqueName="EditCommandColumn1"></EditColumn>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

 asp:sqlDataSource here
Update command, select command, insert command, delete command is here
update, insert, and delete parameters here.

Code behind

namespace Ames5.Schedule
{
    public partial class PWOPENITEMS : System.Web.UI.Page
    {
       
        public string GetConnectionString()
        {

            //sets the connection string from web config file "ConnString"

            return System.Configuration.ConfigurationManager.ConnectionStrings["AMES5DB_TESTConnectionString"].ConnectionString;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SqlConnection conn = new SqlConnection(GetConnectionString());
                SqlCommand cmd = new SqlCommand("Select DISTINCT OPCODE,FLEET,SUBFLEET from  PWOPENITEMS", conn);//query to get the distinct values.

                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sda.Fill(ds, "tbl_data"); //nameing the table getting stored in DataSet as "tbl_data"

                string str1, str2, str3, str4;
                ArrayList MyDisplayList = new ArrayList();
                ArrayList MyValueList = new ArrayList();

                if (ds.Tables["tbl_data"].Rows.Count != 0)
                {
                    MyDisplayList.Add("ALL");
                    foreach (DataRow myRow in ds.Tables["tbl_data"].Rows)
                    {

                        str1 = myRow["OPCODE"].ToString();
                        str2 = myRow["FLEET"].ToString();
                        str3 = myRow["SUBFLEET"].ToString();
                        str4 = str1 + " " + str2 + " " + str3; //Concatinating three strings with space in between.

                        MyDisplayList.Add(str4);
                    }

                    RadComboBox1.DataSource = MyDisplayList;
                    RadComboBox1.DataBind();
                }
                else
                {
                    Response.Write("No Data");

                }

            }

           
            (RadAjaxManager.GetCurrent(this.Page) as RadAjaxManager).ResponseScripts.Add(string.Format("window['gridId'] = '{0}';", RadGridOpenItems.ClientID));

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(GetConnectionString());
            if (RadComboBox1.SelectedItem.Text == "ALL")
            {
                string str = "SELECT * FROM PWOPENITEMS";
                SqlDataSourcePwOpenItems.SelectCommand=str;
               
                    RadGridOpenItems.DataBind();

            }
            else
            {
                string fullString = RadComboBox1.SelectedValue.ToString();

                string firstString = fullString.Substring(0, 3); //Extracting substring

                string middleString = fullString.Substring(3, 7); //Extracting substring

                string lastString = fullString.Substring(11, 1);

               
                string st = "SELECT * FROM PWOPENITEMS WHERE OPCODE = '" + firstString + "' AND FLEET = '" + middleString + "' AND SUBFLEET = '" + lastString + "'";
                SqlDataSourcePwOpenItems.SelectCommand = st;
             
                RadGridOpenItems.Rebind();

            }
        }

        protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            String id = item.GetDataKeyValue("PWOPENITEMS_ID").ToString();

            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                SetMessage("Product with ID " + id + " cannot be updated. Reason: " + e.Exception.Message);
            }
            else
            {
                SetMessage("Product with ID " + id + " is updated!");
            }
        }

        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                SetMessage("Item cannot be inserted. Reason: " + e.Exception.Message);
            }
            else
            {
                SetMessage("New item is inserted!");
            }
        }

        protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            String id = dataItem.GetDataKeyValue("PWOPENITEMS_ID").ToString();

            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                SetMessage("Product with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message);
            }
            else
            {
                SetMessage("Product with ID " + id + " is deleted!");
            }
        }

        private void DisplayMessage(string text)
        {
            RadGridOpenItems.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
        }

        private void SetMessage(string message)
        {
            gridMessage = message;
        }

        private string gridMessage = null;
        protected void RadGrid1_DataBound(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(gridMessage))
            {
                DisplayMessage(gridMessage);
            }
        }

    }
}

 

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 27 Oct 2010, 02:37 PM
Hi Ben,

Indeed, RadGrid is rebound when its items are going in edit mode. However you are setting the new SelectCommand on SelectedIndexChanged of the combo, on the next postback, the SelectCommand is the old one. To overcome the issue you can try setting the SelectCommand on Page_Load to the desired one.

Sincerely yours,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or