简体   繁体   English

从Telerik Radgrid中删除一行。 我如何获得所选行的索引?

[英]Delete a row from Telerik Radgrid. How I get Index of that selected Row?

Here is the code I am using for delete functionality. 这是我用于删除功能的代码。

In RadgridItemdatabound funtion, I have to include this... 在RadgridItemdatabound函数中,我必须包含这个......

foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
                {
                    dataItem["TemplateDeleteColumn"].Attributes.Add("onclick","CellClick('" + dataItem.ItemIndex + "','" + col.UniqueName + "');");
                }

Then I have to create Itemcommand function. 然后我必须创建Itemcommand函数。

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "DeleteSelected")
        {
            GridDataItem item = (GridDataItem)e.Item;
            var itemIndex = item.ItemIndex;
            string LoginId = item.GetDataKeyValue("LoginId").ToString();
            Int32 CampusCode = Convert.ToInt32(item.GetDataKeyValue("CampusCode"));


            Definations def = new Definations();
            Int32 Result = def.deleteUserAssignCampus(LoginId, CampusCode);
            if (Result == 1)
            {
                BindDeptDatasimple();

                cmbColumName.SelectedValue = "";
                cmbDirection.SelectedValue = "";
                Response.Redirect("UserCampus.aspx", false);
                Session["deleteUserCampus"] = "Campus dissociated successfully.";
            }
        }
    }

I could not get index of the selected row in the "var ItemIndex". 我无法获得“var ItemIndex”中所选行的索引。 It always return zero index in ItemIndex. 它总是在ItemIndex中返回零索引。 That's why the first row from the grid gets deleted. 这就是网格中的第一行被删除的原因。 How I can get selected Index of selected row? 我怎么能选择所选行的索引?

The item index will default to 0 because in your case you raise custom command through MasterTableView's client object method. 项索引将默认为0,因为在您的情况下,您通过MasterTableView的客户端对象方法引发自定义命令。

You will have to make sure that in your custom JS function, CellClick , item index is passed into fireCommand client method via the 2nd argument. 您必须确保在自定义JS函数CellClick ,项索引通过第二个参数传递到fireCommand客户端方法。

masterTable.fireCommand("DeleteSelected", itemIndex); 

Then in RadGrid1_ItemCommand event handler, you can retrieve the index value like this 然后在RadGrid1_ItemCommand事件处理程序中,您可以像这样检索索引值

GridDataItem item = (GridDataItem)e.Item;
var itemIndex = e.CommandArgument;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM