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

radgrid edit/delete buttons

20 Answers 2215 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Troika
Top achievements
Rank 1
Troika asked on 03 Aug 2013, 01:01 PM
i have edit/delete auto-generated coluns activated but it shows as a text edit and delete and automatically retrives the info.
how can i have instead of the texto na icon.

i tried this but this gives me na js error saying that need page eventvalidation true.

so there must be a way to but na image in edit delete instead of text

<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" CellSpacing="0" GridLines="None" Skin="Metro" AllowAutomaticDeletes="True" AllowAutomaticUpdates="True" OnItemDataBound="RadGrid1_ItemDataBound">
        <ClientSettings AllowColumnHide="True" AllowKeyboardNavigation="True">
            <Selecting AllowRowSelect="True" />
            <KeyboardNavigationSettings AllowActiveRowCycle="True" AllowSubmitOnEnter="True" />
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
<MasterTableView >
 
    <Columns>
 
         <telerik:GridButtonColumn ConfirmText="Apagar utilizador?" ConfirmDialogType="RadWindow"
 
                    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
 
                    UniqueName="DeleteColumn">
 
                    <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
 
                </telerik:GridButtonColumn>
 
         <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
 
                    <ItemStyle CssClass="MyImageButton"></ItemStyle>
 
                </telerik:GridEditCommandColumn>
    </Columns>
 
 
      <EditFormSettings ColumnNumber="2" CaptionDataField="UserName" CaptionFormatString="Editar propriedades do utilizador {0}"
 
                InsertCaption="New Product">
 
                <FormTableItemStyle Wrap="False"></FormTableItemStyle>
 
                <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
 
                <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3"
 
                    Width="100%"></FormMainTableStyle>
 
                <FormTableStyle CellSpacing="0" CellPadding="2" Height="110px">
 
                </FormTableStyle>
 
                <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
 
                <EditColumn ButtonType="ImageButton"  UpdateText="Actualizar"
 
                    UniqueName="EditCommandColumn1" CancelText="Cancelar">
 
                </EditColumn>
 
                <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
 
            </EditFormSettings>
 
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
 
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column" Created="True">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
 
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
     </EditFormSettings>
 
 
   
 
 
 
<PagerStyle PageSizeControlType="RadComboBox" AlwaysVisible="True"></PagerStyle>
</MasterTableView>
 
<PagerStyle PageSizeControlType="RadComboBox" AlwaysVisible="True"></PagerStyle>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
    </telerik:RadGrid>

20 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Aug 2013, 05:38 AM
Hi Troika,

I guess you want Image button instead of the text.I see that you are using AutoGenerateDeleteColumn="true" AutoGenerateEditColumn="true" along with GridButtonColumn for Delete and GridEditCommandColumn. This is causing the issue.Since you want to set an Image Icon please use only the following code. Remove AutoGenerateDeleteColumn="true" AutoGenerateEditColumn="true" from the code.

ASPX:
<telerik:GridButtonColumn ConfirmText="Apagar utilizador?" ConfirmDialogType="RadWindow" ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
   <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
</telerik:GridButtonColumn>
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
  <ItemStyle CssClass="MyImageButton"></ItemStyle>
</telerik:GridEditCommandColumn>

Hope this helps.
Let me know if any concern.

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 05 Aug 2013, 09:08 AM
i follow your suggest and when i take the auto-generate colums and put that it gaves a javascript error related with  <%@ Page EnableEventValidation="true" %> when i press the icon in the column
0
Princy
Top achievements
Rank 2
answered on 05 Aug 2013, 09:46 AM
Hi Troika,

Make sure that you are using Advanced data binding using NeedDataSource event. Also Paging with disabled control ViewState is not supported. Try setting EnableViewState property of the RadGrid / MasterTableView is set to true.

Thanks,
Princy

0
Troika
Top achievements
Rank 1
answered on 05 Aug 2013, 10:27 AM
ok i need to use that needdatasource event.
sorry i'm new to telerik elememts...

also i have some coluns that shows true/false i i want to replace it with a checkbox, and just allow to change it in edit mode

so i have made this:

<telerik:GridCheckBoxColumn DataField="Aproved" HeaderText="Aproved" SortExpression="Aproved"

UniqueName="Aproved" EditFormColumnIndex="1">

</telerik:GridCheckBoxColumn>--%>



however it instead of replace the actual aproved col adds a news one so i stay with 2 coluns one says true false and other shows the checkbox
I want just the checkbox. same behave with is locked out col.
also in roles I want to show all the roles that exits but just have on checkboxlist maked the one's wich is associated with user
username and created date must be a label  in edit mode.

data must be updated when clicking the update button , how to fetch that event?

also icons are no aligned all it the same way when took the auto.-generated and approved checkbox are not as the others
and how can I take the true false coluns from grid but pay attention that if I take the true false col. the checkbox col will not mark the checkbox so I think I need to hide a coluns by its index

also need command name or similar(because I cant find the property) to make the delete command went in codebehind to delete the row
by the way radgrid will update the data when clickon update button or do I need to specify update command

also I want to hide a user "root" from grid

http://s17.postimg.org/kzv1l7nlb/Sem_T_tulo3.png
0
Princy
Top achievements
Rank 2
answered on 06 Aug 2013, 11:58 AM
Hi,

I'm not clear with all your requirements.Please read through and let me know if any concern.
1)In order to make a checkbox column instead of Approved and Is Locked out,one way is to hide that column in the PreRender event.Please try the following code:

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.GetColumn("ColumnName").Visible = false;        
    }

2)Then on the update button click the UpdateCommand will be fired and you have to write respective code for updation if you are not using Automatic CRUD Operation .

ASPX:
<EditColumn ButtonType="ImageButton" UpdateText="Update" UniqueName="EditCommandColumn1"
    CancelText="Cancel">
</EditColumn>

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
      //Your code for Updation
    }

3)When using the button for delete you can set the CommandName for the function you are performing.Then in ItemCommand or DeleteCommand you can access that button.You can set the HeaderStyle-Width property in the column if not visible fully.
Note that,in case if you are using CommandName as Delete,it perform the delete operation,but if you are giving any other name,it can be accessed only through ItemCommand Event as shown below:

ASPX:
  <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow" ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="DeleteRow" Text="Delete"  UniqueName="DeleteColumn">
     <HeaderStyle Width="100px" />
     <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>
</telerik:GridButtonColumn>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
 {
   if(e.CommandName=="DeleteRow")
    {
       //Code to Delete
    }
 }

4)I see that you want to set two column not to be editable,that is instead of setting it as label.Please try the below code:

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {           
       (e.Item as GridEditableItem)["ColumnName"].Enabled = false;                      
    }
  }

5)If you want to hide a particular row you can use the following code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
     if (e.Item is GridDataItem)
       {
           GridDataItem items=(GridDataItem)e.Item;
           string user=items["UserName"].Text;
           if (user == "root")
           {
               items.Display = false;
           }
       }
   }


I hope these points might help you.
Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 06 Aug 2013, 12:32 PM
on the frist point i'm using that and that hides the checkbox column, because I have two colluns called approved one is the true/false that I want to take and the other is the checkbox...

i'm using this to make the checkbox
 

<telerik:GridCheckBoxColumn DataField="Aproved" HeaderText="Aproved" SortExpression="Aproved"

UniqueName="Aproved" EditFormColumnIndex="1" ItemStyle-HorizontalAlign ="Left">

</telerik:GridCheckBoxColumn>



and then in code behind this

 

dr["Aproved"]

MembershipUser.isAproved

 

so checkboxcloumn is searching from this approved name and transform it to checkbox but can it be binded directly on the above code when creating column


and how can I show in edit mode rolename a checkbox list with all roles but the checkbox just marks if the user is in that role , so I need to take that textboc and replace by checkboxlist

approved checkbox on edit mode is misaligned with other items as you see in image, the update and cancel icons I already align them

code to hide works but it "breaks" the layout because we have a gray cell and then a white with hiding we have two white cells and then a gray one so its easy to understand that is hidden something... any fix?
2) i'm using automatic updates and delete but I'm binding datasource on needdatasource event and when I click on update button nothing happends.
also i'm allowing e-mail change but it must verify before save it if there is an equal email on db it there is pop a message

 

0
Princy
Top achievements
Rank 2
answered on 07 Aug 2013, 06:09 AM
Hi Troika,

Since you are adding the GridCheckBoxColumn declaratively,i don't think it is possible to do from code behind.
Then you want to set a column as checkboxlist in edit mode.Please try the below code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
      foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
       {
           if (col.DataType == typeof(string))
           {
               if (e.Item is GridEditableItem && e.Item.IsInEditMode)
               {
                   GridEditableItem item = (GridEditableItem)e.Item;
                   TextBox txt = (TextBox)item[col.UniqueName].Controls[0];
                   string value = txt.Text;
                   txt.Visible = false;
                   if (col.UniqueName == "RoleName")
                   {
                       CheckBoxList checks = new CheckBoxList();
                       checks.SelectedValue = value;
                       checks.DataSourceID = "SqlDataSource1";
                       checks.DataTextField = "RoleName";
                       checks.DataValueField = "RoleName";
                       checks.ID = "lnk" + Guid.NewGuid();
                       item[col.UniqueName].Controls.Add(checks);
                   }
               }
           }
       }       
   }

Then your issue on misalignment of the approved column is because you have set EditFormColumnIndex="1",please remove that and see or make its value "0" to get the alignment correct.

Then when using Automatic CRUD operation make sure that you do according to what is shown in this demo.crud means create,read,update and delete.Automatic crud means without handling anything manually from code behind.

Then in order to verify the email you could try querying the database into the InsertCommand and check if the value into the email variable already exists into some record. If it exists you could cancel the InsertCommand.

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 07 Aug 2013, 08:31 AM
ok  will try.
what about the style "break" while hiding a row because the normal view is one line gray and one white now I have two whites because one gray is hidden.
another thing if I take that checkbox declarative could I add it in code-behind and make it check if approved or not and hide the true false col?

so I need to add the checkbox and do not enable it in item template but only in edit mode  that why I think checkgridcoulumn its easy
0
Troika
Top achievements
Rank 1
answered on 07 Aug 2013, 01:40 PM
how do I get checkboxlist checked items so save on update event. I found this but I think isn't appropriate

for (int i = 0; i < clbIncludes.Items.Count; i++) if (clbIncludes.GetItemCheckState(i) == CheckState.Checked) // Do selected stuff else // Do unselected stuff
0
Troika
Top achievements
Rank 1
answered on 08 Aug 2013, 10:04 AM
I have a datarow with isaproved so I need to change it to checkbox instead of true/false

if (e.Item is GridDataItem)

{

GridDataItem item = (GridDataItem)e.Item;

if ((col.UniqueName == "Aproved") || (col.UniqueName == "IsLockedOut"))

{

CheckBox cb = new CheckBox();

cb.ID = "lnk" + Guid.NewGuid();

item[col.UniqueName].Controls.Add(cb);

}


now I need to fetch the value of it like gridcheckboc clomun does and hide that true/false
0
Princy
Top achievements
Rank 2
answered on 08 Aug 2013, 10:11 AM
Hi Troika,

You mentioned you are using Automatic CRUD operation, then I'm not sure about the using of UpdateCommand in your code,for you requirements,its better to apply Manual CRUD operation.Please try the below code to get the checkboxlist selected data in update command.

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
   {
       string values=string.Empty;
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           GridEditableItem data = (GridEditableItem)e.Item;
           CheckBoxList checkedItems = (CheckBoxList)data.FindControl("CheckBoxList1");//Accessing the CheckBoxList
           for (int i = 0; i <checkedItems.Items.Count; i++)
           {
               if (chk.Items[i].Selected)
               {                 
                   values = values + "," + checkedItems .Items[i].Text.ToString();//Storing the selected values
               }
           }
         string selectedvalue = values.Trim(',');//To trim off the last ','          
       }
   }
Make sure you have set the CheckBoxList Id=CheckBoxList1,when creating the CheckBoxList.

To access the CheckBoxColumn in editmode in code behind you can try the below code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editdata = (GridEditableItem)e.Item;
        CheckBox check = (CheckBox)editdata["Aproved"].Controls[0];
        if (check.Checked)
        {
         //Your Code
        }
     }
}


Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 08 Aug 2013, 02:28 PM
ok I will try then I have this code bellow wich as an "aproved" checkbox col. I and need to check it depending on is approved or notand I bind it with custom datasource take I build I code-behind so I need to just get it after read the datasource .

so if I make this:

<telerik:GridCheckBoxColumn DataField="Aproved" HeaderText="Aproved" SortExpression="Aproved"

UniqueName="Aproved" >

 

 

 

</telerik:GridCheckBoxColumn>

it create a checkbox an auto-checks if true or not
when I change to this:

I think I need to set client.idmode to static in code behind of that checkbox list otherwise when o run that code to check the values in update command it gives Object reference not set to an instance of an object."} because I have a content place holder

if (e.Item is GridDataItem)

 

 

{

 

GridDataItem item = (GridDataItem)e.Item;

if ((col.UniqueName == "Aproved") || (col.UniqueName == "IsLockedOut"))

 

 

{

 

CheckBox cb = new CheckBox();

cb.ID = "lnk" + Guid.NewGuid();

 

 

item[col.UniqueName].Controls.Add(cb);

}

 

 

the checkbox column instead of true/false appears the checkbox but no one is marked with the check so its not getting my custom datasource

with frist option it leaves the true/false column and add another one called also approved with checkbox

 

 

 

0
Princy
Top achievements
Rank 2
answered on 09 Aug 2013, 04:39 AM
Hi Troika,

GridCheckBoxColumn by its nature will be disabled in the view mode. And will be enabled in the edit mode. From your code I have seen that you are assigning a DataField="Aproved" for the GridCheckBoxColumn. So the GridCheckBox column will be either checked or unchecked depending o the value of the DataField. In case if you want to change the value, you can do it in edit mode and update to database.

I am not sure about what you want to accomplish. Can you please specify the following,
1. Why you are adding a CheckBox to the 'Aproved' and 'IsLockedOut'?
2.What do you mean by the custom datasource.

 Here is the complete code I have tried to add CheckBoxList to the field RoleName in edit mode and retrieving its value in UpdateCommand.Please change the "cb.ID = "lnk" + Guid.NewGuid();" to "CheckBoxList1" as I have mentioned in the above code and try the below code snippet.

C#:
//To create the CheckBoxList in Edit Mode
 protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
        {
            if (col.DataType == typeof(string))
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    TextBox txt = (TextBox)item[col.UniqueName].Controls[0];//Access the textbox in editmode
                    string value = txt.Text;
                    if (col.UniqueName == "RoleName")
                    {
                        txt.Visible = false;
                        CheckBoxList checks = new CheckBoxList();
                        checks.DataSourceID = "SqlDataSource1";
                        checks.DataTextField = "RoleName";
                        checks.DataValueField = "RoleName";
                        checks.ID = "CheckBoxList1";                     
                        item[col.UniqueName].Controls.Add(checks);
                    }
                    else
                    {
                         txt.Visible = true;
                    }
                }
            }
        }
    }
//To make the CheckBoxList select the respective value
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
        {
            if (col.DataType == typeof(string))
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    TextBox txt = (TextBox)item[col.UniqueName].Controls[0];
                    string value = txt.Text;                   
                    if (col.UniqueName == "RoleName")
                    {
                        txt.Visible = false;
                        CheckBoxList chk = (CheckBoxList)item.FindControl("CheckBoxList1");
                        chk.SelectedValue = value;
                    }
                }
            }
        }
 }
//Update command to get the values of CheckBoxList
  protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        string values=string.Empty;
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem data = (GridEditableItem)e.Item;
            CheckBoxList chk = (CheckBoxList)data.FindControl("CheckBoxList1");
            for (int i = 0; i < chk.Items.Count; i++)
            {
                if (chk.Items[i].Selected)
                {                  
                    values = values + "," + chk.Items[i].Text.ToString();//Storing the selected values
                 }
            }
            string selectedvalue = values.Trim(',');               
        }
    }

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 09 Aug 2013, 09:11 AM
ok I will explain better...

I have this custom dataset http://forums.asp.net/t/1887907.aspx/1 and the problem is it creates an colluns called approved with true/false instead of checkbox. if I create a checkbox col manually it replace that column with the checkbox but don't bind it with any value (checked/unchecked) juts in edit mode.

if I add a gridcheckboc clomun it will stay will the true/false column and this new one with the checkboxes here they showed on the gridtemplateand edit mode with values in both.
I could try to hide a column by its index, since in the last option the two column have same name. how could I hide it

also I need to fix when hide a row it insetad of make a row gray and white it stays with 2 white

how could then I fetch the email etxtbox and this checkbox clomun values in update?
also the checkboxlist gives me an error I think I have to use clientidmode because I have all things inside a content place holder and this could cause the error
I have changed the name to CBLRole but it gives me that error maybe because is in contentplaceholder
0
Troika
Top achievements
Rank 1
answered on 09 Aug 2013, 09:58 AM
update

I have add a gridcheckbox column and hide on pre-render the other column that fine but in edit mode the other one stills appear.
the way to get checkboxlist checks still get error on update button click
need to fetch textbox and this new checks on update also.
if you know other way to make the checkbox work like gridcheckbox col(bind it and just editable in edit mode) because as I said before ifi add it manually it wont bind any value in template just on edit mode

update2.
it wont find CBLRole because it needs to use client.idmode=static but I don't know how to set it in code-behind since client id property don't give any option, now it gives this:

ctl00_ContentPlaceHolderMain_RadGrid1_ctl00_ctl07_CBLRole_0


update3

I have added

checks.ClientIDMode = ClientIDMode.Static;

 but it enumerates the options like cblrole_1 ; cblrole_2 etc and don't finde cblrole
0
Troika
Top achievements
Rank 1
answered on 12 Aug 2013, 09:49 AM
any help onthis because cant find it I think its because of the content pace holder wich has the grid
0
Princy
Top achievements
Rank 2
answered on 12 Aug 2013, 01:23 PM
Hi Troika,

In order to hide the column,please try the following code snippet.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
    // Hide Column in view mode
    RadGrid1.MasterTableView.GetColumn("Aproved").Visible = false;   
   }
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
     //Hide Column in edit mode
     if (e.Item.IsInEditMode && e.Item is GridEditFormItem)
      {
       (e.Item as GridEditableItem)["Aproved"].Parent.Visible = false;
      }
   }

The rest of the issue ,i wasn't able to replicate,its working fine at my end,even after placing in place holder.Can you share the full code you have tried,so that your requirements are more clear and help you out with the issues.

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 13 Aug 2013, 10:41 AM
i have alredy hide the column.
now its just the problema to fetch the data . and the "style break" problem when hidding a user it stays with two rows in white instead of one white and one gray.

how can i fetch teh data in the textbox in edit mode so there i can understtod if the problwm is with that part of the checkboxlist or not
0
Princy
Top achievements
Rank 2
answered on 13 Aug 2013, 11:57 AM
Hi Troika,

Please have a look into this documentation on Accessing Cells and Rows ,it shows how to fetch the data in textbox in edit mode.

Hope this helps you.

Thanks,
Princy

0
Troika
Top achievements
Rank 1
answered on 14 Aug 2013, 09:17 AM
i have take a look at the generated code and the name is direferent not just cblRole i think thats why it dont find also i cant found the textbox <input name="ctl00$ContentPlaceHolderMain$RadGrid1$ctl00$ctl09$CBLRole$2" id="ctl00_ContentPlaceHolderMain_RadGrid1_ctl00_ctl09_CBLRole_2" type="checkbox" checked="checked" value="Utilizadores"/>
Tags
Grid
Asked by
Troika
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Troika
Top achievements
Rank 1
Share this question
or