Remove Edit, Update, and Cancel links from a RadGrid - c#

I am trying to remove the links that are displayed in a Telerik RadGrid by default. Here is what the grid looks like before I try to remove the edit link:
I have found this snippet of code, it is used to remove the edit link:
if (!IsPostBack)
{
foreach (GridItem item in RGV_POI.MasterTableView.Items)
{
if (item is GridEditableItem)
{
GridEditableItem editableItem = item as GridDataItem;
editableItem.Edit = true;
}
}
RGV_POI.Rebind();
}
This is how the grid looks after the code:
The edit link still shows up on the first item. Is there a way to remove the edit, update, and cancel link on each item in the RadGrid? I want to be able to remove/disable the links, using a button click event. Then be able to add/enable the links back, using a button click event.

I'm not aware of Telerik RadGrid Control, but for sure the control should inherit asp:GridView. You can make links not visible in RowDataBound event. Here how you can do it.
Add OnItemDataBound="Grid_ItemDataBound" on the grid view.
In the code behind:
protected void Grid_ItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Item.DataItem == null)
return;
//cell of all the link button edit/update etc.
TableCell cell = e.Item.Cells[//index of the column];
foreach(Control c in cell.Controls)
{
c.Visible = false;
}
}
You should check the ID of the cancel, edit, update buttons somehow. Probably you should give more information about the controls in the aspx.
EDIT:
Use OnItemDataBound event it existing in their documentation: http://www.telerik.com/help/aspnet-ajax/events_t_telerik_web_ui_radgrid.html

The edit link button in a RadGrid is actually a column itself, specifically a GridEditCommandColumn. In order to show/hide this in the event of a button click, you would have to essentially rebuild all the columns programatically in the click event handler, including or excluding the GridEditCommandColumn as needed. You cannot add or remove a single column programatically when the rest of the grid is created declaratively. It would be useful if we could see more of how the grid is declared and built in your application.
Creating a RadGrid Programatically
It may be possible to change the GridEditCommandColumn.Display property, however. If you can get a handle on the column itself, instead of the individual cells, you may be able to adjust this as needed in your button click events.

You should remove the GridEditCommandColumn if you do not want your items editable. Another option is to change its visibility on the server through its Visible/Display property. You can use the grid's GetColumnSafe(columnName) method to get the neded reference: http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html
To get rid of the update/cancel buttons, you can use a custom template, although I do not see why you would need to do that if your grid is not editable: http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx

Related

ComboBox Selected Value to be Blank

Uses: VS 2012;
I've a combobox attached to a datasource in my form. And things work fine. When I run the form, again everything works fine; I can select an item in the dropdown list and it updates to the datasource as well. My problem comes when I need to deselect/revert what I have selected after I saved or Remove what I have select (basically should go as null for that field value).
Our legacy system was built in Delphi 3 & 5, and users got a feature of right-clicking on the dropdown list and get a small popup like button named
Blank
which blanks what have been selected. I could not find anything that will do the same what ever user have selected in .NET's combo box.
You can add a new item in dropdown named -Select-( or something similar name) by using following code:
drp.DataSource = dataSet;
drp.DataBind();
// do it after binding
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
If you are binding in xaml then on page_load event you can write only this line
drp.Items.Insert(0, new ListItem("-Select-", "NA"));
Now if user want to deselect choice, he/she will simply select -Select- item.
Whilst thanking all of your Answers and suggestions, I used #V4Vendetta's idea and composed my solution.
Similarly to deleting a record in a datagridview where you click Delete key, I took the same concept and associated my solution with Delete Key.
What I did was created a handler for ComboBox's Keypress Event like:
private void comboBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
(sender as ComboBox).SelectedIndex = -1;
}
}
And linked to every ComboBox's available
ComboBox1.KeyDown += new KeyEventHandler(comboBox_KeyPress);
ComboBox2.KeyDown += new KeyEventHandler(comboBox_KeyPress);
Now when user clicks Delete key while the ComboBox selected/active, it gets blank.

Command behind a GridView ButtonField

I got a question about the command behind a ButtonField (image type) in a GridView.
Got a GridView called gvIngevuld in wich I show rows of data and 1 ButtonField row.
Now I have to put code behind these buttons (Each of them the same) to put some things in PDF format.
The problem is I don't know how to put code behind these buttons ?
The code I have :
<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png" CommandName="pdf_click" />
As you can see I used the CommandName="pdf_click" property but when I click one of the buttons there's only a postback but nothing happens.
Anyone who can help me out here ?
in case needed, code behind :
protected void pdf_click(object sender, EventArgs e)
{
lblWelkom.Text = "Succes!";
}
Thanks.
You should use RowCommand event of gridview. Set the commandArgument to your item's unique key. (By default, it passes the row's index)
void gvIngevuld_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="pdf_click")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = gvIngevuld.Rows[index];
//gbIngevuld is your GridView's name
// Now you have access to the gridviewrow.
}
}
Also, If you have set the DataKeyNames of the gridview, you can access it as follows:
int index = Convert.ToInt32(e.CommandArgument);
int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);
Just an addition, I wrote and tried exactly the same however the button still not working...
...it took some time till I realise that I forgot a thing, which was
OnRowCommand="<YourGridViewName>_RowCommand"
inside your GridView element in the **.cs* file
The CommandName property does not define the method to call. For that you need to create a method that is bound to the command event of the control.
I don't know too much about about GridViews i'm afraid but I would think the easiest way to do this would be to select it in design view in Visual Studio, go to properties, click on the events button (looks like a lightning flash) and double click on the RowCommand event propety. This should automatically create a method that is bound to a command button event.
You can then use the GridViewCommandEventArgs parameter to access the CommandName, CommandArgument and CommandSource properties to work out which buttom caused the event to fire.

XtraGrid Questions

I have follwoing feew questions on XtraGrid (Dev Express).
How to enable editing the cell by double clicking on it? by defalut XtraGrid permits cell editing if we just click on it. I dont want this to be happen.
How do I get the column/Row information which is edited?. Is there any event like AfterRowEdit() or AfterCellEdit()?
Thanks,
Omkar
1 You could capture the click event and enable the editor if its clicked twice in a short timespan.
2 To get column/row information I would add a special editor to the column and capture its events.
Try setting the OptionsBehaviour.EditorShowMode property of your view to MouseDownFocused. That way the user has to focus the cell first, and the editor will show up only on second click.
Have a look at view's ValidateRow event, or if you need any processing BEFORE editing a row, you could use the view's ShowingEditor event, and grab the actual row by the view's FocusedRowHandle property.
Disable the gridview editor.
capture DoubleClick event on the gridcontrol.
And in this event enable the gridview editor
===========
Bind each column to a repository item
Go to a column and find the columnedit property.
Set a repository item to that column.
Then assign the validating event to the repository item.
Code:
private void your_gridcontrol_double_click(object sender, EventArgs e)
{
GridHitInfo hit = your_gridview.CalcHitInfo((e as MouseEventArgs).Location);
if (hit.InRow)
{
}
}

Cannot select checkbox in a databounded DataGridView

I've a DataGridView which is working perfectly fine. I use it just to show data.
Now I want ability to select rows by check box and perform an operation for only selected rows on click of a button (this button is out of the grid on the same form).
For this purpose, I'm following these steps to add checkbox column to datagridview.
On running the application what is see is I can't check the check box either by mouse click or keyboard. And by its looks I can understand that its not in disabled/readonly state. So whenever I try to click on the checkbox, it changes it's borders normally as an enabled check box does. But finally it is not checking the check box.
Try it.
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn ck = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Insert(0,ck);
}
may help you.
Ismail here is your solution of your confusion Dgv-DatabindingCompleteEvent
I went through the same problem. For me the solution was pretty simple. My datagridview had a disabled editing option (because I didn't want the user to change the data) and I wanted to be able to check/uncheck my DataGridViewCheckBoxColumn. So in the dataGridView properties I checked the 'Enable Editing' option, but in the code I've disabled it for every column except of mine desired checkBoxColumn.
Hope it will help to somebody.
if you want to check the state of all checkBoxes in the dgv:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chk.Value) == true)
MessageBox.Show("this cell checked");
}
In my case I had the gridview loading code in page_load, so after a button click the grid was reloading and lost its selection. So Moving the code inside
if(!isPostback)
{ LoadGrid();}
worked for me.
Put a breakpoint in page_load to understand better.

How to add a reference to a method in a custom control to a child control created in that same custom control

I have a custom control that is based off the gridview control located at: here
The control is basically a gridview that automatically creates a column of checkboxes that can be used to "check" individual rows in the gridview.
During the gridview's "CreateColumns" event, the "checkboxcolumn" is created dynamically. The checkboxcolumn also contains another checkbox in the header that is used to "select/deselect all" checkboxes in the column.
Since the gridview does not automatically remember the state of the checkboxes in the checkboxcolumn on postback, I added a method to the control called "SaveCheckBoxState" which stores the indexes of the checked rows in Viewstate, and then I modified the "OnRowDataBound" event to check the Viewstate and reset the checkboxes based on the Viewstate.
I then added a call to "SaveCheckBoxState" in the gridview's OnSorting and OnPageIndexChanging events. This works great so long as I'm sorting or changing pages.
However, I need it to update the viewstate everytime someone clicks or unclicks one of the checkboxes. At this time, the checkboxes are rendered with an onclick event that calls some javascript to highlight the row, or in the case of the checkbox in the header, to select/deselect all checkboxes.
I need to call the "SaveCheckBoxState" method from the javascript used by the customcontrol, or I need to find a way to modify viewstate from javascript and perform the same action as "SaveCheckBoxState".
I've tried adding the "SaveCheckBoxState" to the onclick event declaration in the checkboxes, but when run, it simply tells me that the method is undefined. It doesn't exist in the parent page, and I don't think I should have to make an event for the parent page to pass the click to. It seems to me this should be all self contained within the custom control.
Does anyone know how I can acheive this?
Here is the code for the gridview OnPreRender event where the onclick event of the checkbox is set:
protected override void OnPreRender(EventArgs e)
{
// Do as usual
base.OnPreRender(e);
// Adjust each data row
foreach (GridViewRow r in Rows)
{
// Get the appropriate style object for the row
TableItemStyle style = GetRowStyleFromState(r.RowState);
// Retrieve the reference to the checkbox
CheckBox cb = (CheckBox)r.FindControl(InputCheckBoxField.CheckBoxID);
// Build the ID of the checkbox in the header
string headerCheckBoxID = String.Format(CheckBoxColumHeaderID, ClientID);
// Add script code to enable selection
cb.Attributes["onclick"] = String.Format("ApplyStyle(this, '{0}', '{1}', '{2}')",
SelectedRowStyle.CssClass,
style.CssClass,
headerCheckBoxID);
// Update the style of the checkbox if checked
if (cb.Checked)
{
r.BackColor = SelectedRowStyle.BackColor;
r.ForeColor = SelectedRowStyle.ForeColor;
r.Font.Bold = SelectedRowStyle.Font.Bold;
}
else
{
r.BackColor = style.BackColor;
r.ForeColor = style.ForeColor;
r.Font.Bold = style.Font.Bold;
}
}
}
You should have your custom GridView control subscribe to the CheckChanged event of the checkboxes. It should call its SaveCheckBoxState method on its own. That method should be private.
Parent and child controls should only communicate via properties and events, never through methods and fields.

Categories