how to get the column index of the gridview - c#

I have two image buttons in the GridView used for selection. They populate the textboxes. What I need is when i click imagebutton "Edit" it should populate the textboxes from the gridview and when the imagebutton "Delete" is pressed it does the same and disbale the textboxes too. The point is am not getting a logic of how to get the column indexes programmatically.
help me out with the condition.
the issue is just with the c# code. Here is the snippet which i have tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var row = GridView1.SelectedRow;
if (condition)
{
_PropertyTitle.Text = row.Cells[1].Text;
_Address.Text = row.Cells[2].Text;
}
else
{
_PropertyTitle.Text = row.Cells[1].Text;
_PropertyTitle.Enabled = false;
_Address.Text = row.Cells[2].Text;
_Address.Enabled = false;
}
}
}

It is not very clear from question that what you are trying to achieve.But what i got is you want to write specify both image button click separately.
for that you have to use the RowCommand event.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
In this row command event according to CommandName (of button) you can manage the code for two buttons
Refer this link for Rowcommand event row command
One more link

Related

Cannot change button properties in C# Webform GridView _RowEditing event

I have a GridView object on a C# WebForm page which displays table data and allows editing. I have made a Template Button column to execute the Update Command in my SQLDataSource Object.
The buttons are enabled by default, which caused errors when they were pressed without the Row being in Edit Mode. So I used the GridView1_DataBound() Event to disable the buttons for each row using FindControl on the appropriate column. This works fine.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
Button setButton = (Button)gvrow.Cells[7].FindControl("Button1");
setButton.Visible = False;
}
}
What I want to do is when a row is selected for editing, I enable only that row's button. I have been able to use FindControl to get a reference to the button (verified I have the reference doing Debug) But when I make changes to the button, it doesn't work.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataBind();
int c = GridView1.Rows[e.NewEditIndex].Cells.Count - 1;
Button setButton = (Button) GridView1.Rows[e.NewEditIndex].Cells[c].FindControl("Button1");
// doesn't work!!
setButton.Visible = true;
}
In debug mode, I verified that I do have access to the button for the row and can see its properties, etc.
I have tried changing forecolor, text, etc. but nothing changes on the actual button; what do I need to do in order to change the button?

How to get selected row index in devexpress gridcontrol?

I have devexpress gridcontrol which looks like that:
I have click event on this red X button:
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
}
How can get there row index where this button is ?
You cannot access rows on GridControl, since this is just a container for the views.
As I can see from your picture you're using GridView. When you press the delete button, focused row changes and you can access it via FocusedRowHandle.
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
var gv = myGridControl.MainView as GridView;
var index = gv.FocusedRowHandle;
gv.DeleteRow(index);
}
You can use the GridView.FocusedRowHandle property:
view.DeleteRow(view.FocusedRowHandle);

Get data from a specific column when pressing edit on that row in GridView

How do I get data from a specific column that the edit button was pressed on in a gridview?
The following code doesnt work:
protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = viewStoryTime.SelectedRow.Cells[0].Text;
}
This is because the row is not actually selected. How can I accomplish this?
e.NewEditIndex has the row index of the currently-editing row. You can use that to access the row and read the cell data as necessary.
The parameter GridViewEditEventArgs contains the row index for the current edited row from the gridview.
You should do something like this
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = MyGridView.Rows[e.NewEditIndex].Cells[0].Text;
Another way could be implement a RowCommand event where parameter GridViewCommandEventArgs carries the command name, then you should do something like this:
void MyGridView_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=="my command name")
{
//Do stuff here
}
}

Get the ID of the Grid

I have two ASP.NET Grid views, which contain an Image-button, which both call an Edit On-click event. the On-click event looks like:
protected void Edit(object sender, EventArgs e)
{
ImageButton ibtn1 = sender as ImageButton;
using (GridViewRow row = (GridViewRow)((ImageButton)sender).Parent.Parent)
{
txtMessageID.ReadOnly = true;
txtMessageID.Text = row.Cells[2].Text;
txtReference.Text = row.Cells[6].Text;
buttonClicked.Text = ibtn1.ID.ToString();
popup.Show();
}
}
which sole purpose is to fire off a ModalDialogBox, with key items from the grid view being clicked. My problem is that one of the grids doesn't have the Cells[6] (Reference) and therefore falls over. What i need to do is wrap a statement around this cell checking which grid (ID) the button click came from.
I'm not using Row-command, as this wouldn't allow for a single method call from multiple grids. My question is how do I obtain the Grid ID from the Image Button being clicked within this method (see above)?
ended up using the RowCommand of the Gridview, and then used the following to get what i need:
rotected void Edit(object sender, GridViewCommandEventArgs e)
{
ImageButton ibtn1 = sender as ImageButton;
GridView grd = sender as GridView;
string gridName = grd.ClientID;
string buttonId = e.CommandName;
using (GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer)
{
txtMessageID.ReadOnly = true;
txtMessageID.Text = row.Cells[2].Text;
if (gridName == "grdMessageDups")
{
txtReference.Text = row.Cells[6].Text;
}
buttonClicked.Text = ibtn1.ID.ToString();
popup.Show();
}
}

The GridView 'GridView1' fired event RowDeleting which wasn't handled , but there is an event

ı have been traying to create dynamic control panel using webusercontrol , delegate,and ADO.
Even ı have write the delegate for deleting and editing ı faced with "The GridView 'GridView1' fired event RowDeleting which wasn't handled."problem . Can anybody help me pls
here is my codes
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = this.DataSource;
GridView1.DataBind();
GridView1.DataKeyNames = new string[] { this.DataKeyNames };
}
public object DataSource { get; set; }
public string DataKeyNames { get; set; }
public event GridHander RowDeleting;
public event GridHander RowSawing;
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvr = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
int rowIndex = gvr.RowIndex;
object id = GridView1.DataKeys[rowIndex].Value;
switch (e.CommandName)
{
case "Edit":
GridView1.EditIndex = rowIndex;
break;
case "Delete":
if (RowDeleting != null)
{
GridEventArgs args = new GridEventArgs()
{
row=gvr,
id=id,
rowIndex=rowIndex
};
RowDeleting.Invoke(e.CommandSource, args);
}
break;
case"Save":
if (RowSawing != null)
{
GridEventArgs args = new GridEventArgs()
{
row = gvr,
id = id,
rowIndex = rowIndex
};
RowSawing.Invoke(e.CommandSource, args);
}
GridView1.EditIndex = -1;
break;
case "Cancel":
GridView1.EditIndex = -1;
break;
default:
break;
}
}
}
//My webform
ublic partial class CategoryControlPanel : System.Web.UI.Page
{
CategoryResposite _categoryResposite=new CategoryResposite();
protected void Page_Load(object sender, EventArgs e)
{
ControlPanel.DataSource = _categoryResposite.ListCategories();
ControlPanel.RowDeleting += ControlPanel_RowDeleting;
ControlPanel.RowSawing += ControlPanel_RowSawing;
}
void ControlPanel_RowSawing(object sender, GridEventArgs e)
{
throw new NotImplementedException();
}
void ControlPanel_RowDeleting(object sender, GridEventArgs e)
{
_categoryResposite.DeleteCategories(Convert.ToInt32(e.id));
}
You are trying with a command name of Delete for your delete button. So the gridview creates a row deleting event automatically....
You need to change the command argument from Delete to something else like Delete_Product or whatever you want...
The code that you have posted is incomplete (missing the aspx file code), from your description of the problem it sounds as though you have not assigned the RowDeleting event to GridView1.
Inside the opening gridview tag in the aspx file add the assignment as follows:
<asp:gridview ID="..." runat="server" ... OnRowDeleting="<name of event handler>" ...>
If the event handler ControlPanel_RowDeleting is designed to handle a delete from gridview action then insert this as the event handler name. Ensure that you re-bind the gridview after delete so that the changes are shown on postback.
protected void ControlPanel_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// cancel the automatic delete action
e.Cancel = true;
// do the delete
_categoryResposite.DeleteCategories(Convert.ToInt32(e.id));
// complete delete action
GridView1.DataBind();
}
One of the good things about GridView is that it provides a built-in CommandField Buttons which allows us to perform certain actions like editing, updating,deleting and selecting of GridView data.
To add those command fields mentioned in the GridView you can follow these few steps below:
1. Switch to Design View
2. Right Click on the GridView and Select --> Show Smart Tag --> Add New Columns
3. On the List Select CommandField
4. Check Delete and Edit/Update options then OK
As you can see the Edit and Delete CommandField are automatically added in the last column of GridView. Now we can start to write our codes for editing and updating the information in the GridView.
In-order to perform Edit and Update in GridView we need to use three events ( GridView_RowEditing, GridView_RowCancelingEdit , GridView_RowUpdating). For those who do not know on how to generate Events in GridView you can follow these steps below:
Switch to Design View in Visual Studio Designer
Click on the GridView
Navigate to the GridView Property Pane and then SWITCH to Event Properties
From there you would be able to find the list of events including those three events mentioned above
Double Click on that to generate the Event handler for you
Try adding protected to the method signatures.

Categories