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

Template column of Checkbox and Delete button

6 Answers 167 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Koi
Top achievements
Rank 1
Koi asked on 03 Dec 2010, 12:50 PM
Hello All,

In my RadGrid, I have a template column of checkbox .
- If any checkbox is checked  then click "delete" button to delete that row (just change status from 'Active' to 'Inactive', not delete that row from database),
  the status of that rows must changed to 'Inactive' (First time, All of rows have 'Active' Status)

In Web controls I can code but in RadControl I can't because of I've never used these controls before.

Plz help me..

This is my code in aspx.vb
------------------------------------------------------------------------------------------------------
Protected Sub btnMultipleRowDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = 0
For Each row As GridViewRow In GridView1.Rows
Dim MatId As String = Convert.ToString(GridView1.DataKeys(row.RowIndex).Value)
Dim checkbox As CheckBox = CType(row.FindControl("CheckBox1"), CheckBox)
If checkbox.Checked Then
i = i + 1
Dim a As String
a = GridView1.DataKeys(row.RowIndex).Values("MatStatus").ToString()

SqlMaterial.UpdateCommand = "Update Material SET [MatStatus] = 'Inactive' Where MatId ='" + MatId + "'"
SqlMaterial.Update()

Dim sb As New StringBuilder
sb.Append("<script type='text/javascript' language='javascript'>")
sb.Append("alert('Delete Complete');")
'sb.Append("document.location ='manageMatTypeData.aspx';")
sb.Append("</script>")
Page.ClientScript.RegisterStartupScript(Page.GetType, "RedirectScript", sb.ToString)
End If

Next row
If (i = 0) Then
Page.ClientScript.RegisterStartupScript(Page.GetType, "RedirectScript", "<script language='javascript'>alert('Please select the row you want to delete');</script>")

End If

End Sub
-------------------------------------------------------------------------------------------
Thanks very much,
Koi

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Dec 2010, 01:14 PM
Hello,

I guess you have "Delete" button in CommandItemTemplate. You could iterate through the items and get the CheckBox control to set the status to "InActive".

C#:
protected void Button1_Click(object sender, EventArgs e)
 {
     foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
     {
         CheckBox chkBox = (CheckBox)item.FindControl("CheckBox1"); // CheckBox1 is in ItemTemplate
         chkBox.Checked = true// Your logic here
     }
     
 }



-Shinu.
0
Koi
Top achievements
Rank 1
answered on 03 Dec 2010, 01:27 PM
Thanks very much, Shinu

But it has an error
*** Type 'GridDataItem' is not Defined ****
Actually, I try to solve this problem all day but icant... -_-

and when i ran the program , this message appeared
----------------------------------------------------------------
System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'System.Web.UI.WebControls.GridViewRow'."
Source="TestAjax"
StackTrace:
at TestAjax.Test1.btnMultipleRowDelete_Click2(Object sender, EventArgs e) in C:\Users\Koi\Desktop\Web\TestAjax\TestAjax\Test1.aspx.vb:line 6
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:

---------------------------------------------------------------

Thanks ,
Koi
0
Shinu
Top achievements
Rank 2
answered on 04 Dec 2010, 07:55 AM
Hello Koi,


I assume, you have not added the reference to Telerik namespace in the code page.
         using Telerik.Web.UI;

        and that's the cause of getting the error "Type 'GridDataItem' is not Defined ".


Try the same code after adding the Telerik namespace reference.



-Shinu.
0
Koi
Top achievements
Rank 1
answered on 04 Dec 2010, 11:52 AM

Hello Shinu,
I added the Telerik namespace reference in the code page already and the error disappeared...

However,  it has other problems...

I'm very confuse how to convert datatype from int to string -->  "MatId"
and How to get string value from --> "a"
I think that their syntax aren't correct.
Pls help me again...

Protected Sub btnMultipleRowDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonGW2.Click
        Dim i As Integer = 0
        For Each item As GridDataItem In RadGrid1.MasterTableView.Items
            Dim checkbox As CheckBox = DirectCast(item.FindControl("CheckBox1"), CheckBox)
                              
            Dim MatId As String = Convert.ToString(RadGrid1.MasterTableView.Items(item.ItemIndex))
  
            If checkbox.Checked = True Then
                i = i + 1
  
                Dim a As String
  
                a = item("MatTypeMinorStatus").ToString()
  
                SqlDataSource1.UpdateCommand = "Update MaterialTypeMinor SET [MatTypeMinorStatus] = 'Inactive' Where Mattypesubid ='" + MatId + "'"
                SqlDataSource1.Update()
...................................................

Thank you very much,
Koi
0
Shinu
Top achievements
Rank 2
answered on 06 Dec 2010, 06:06 AM
Hello Koi,

If you wants to get item index as the value of 'MatId', try the following code snippet.

Vb.Net:
For Each item As GridDataItem In RadGrid1.MasterTableView.Items
   Dim MatId As String = Convert.ToString(item.ItemIndex)
Next

And try the following code snippet to get the cell value for column 'MatTypeMinorStatus'.

Vb.Net:
Dim a As String = item("MatTypeMinorStatus").Text

-Shinu.
0
Koi
Top achievements
Rank 1
answered on 06 Dec 2010, 08:17 AM
Thank you very much for your help, Shinu.
^_^
Tags
Grid
Asked by
Koi
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Koi
Top achievements
Rank 1
Share this question
or