How to get datakeys value in a gridview - asp.net

I try to delete a record in Gridview and Database .I added a boundfield button and set the CommandName to Deleterecord in the RowCommand event I want to get the primary key of this record to delete that(primary key value does not show in the grid view. The following block of code shows this event(I try to show some data in a text box):
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
TextBox1.Text = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
//bla bla
}
}
I also set
DataKeyName="sourceName"
in the gridview but it is not my primary key
If I click this button an exception occured like this:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How can I solve this problem

Use this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
TextBox1.Text = grid.DataKeys[gvr.RowIndex].Value.ToString();
//bla bla
}
}
Here LinkButton should be replaced by the type of control which is performing this RowCommand Event
For more info go Here

Related

get row value on update Button

get the Product SKU Of the row which the button pressed
protected void On_Update_Click(object sender, EventArgs e)
{
}
The easiest way is to use the DataKeyNames property.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SKU">
And then you can access the correct DataKeyNames based on the Row Index.
protected void On_Update_Click(object sender, EventArgs e)
{
//cast the sender back to a control and get the gridviewrow from the namingconainer
GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
//get the gridview itself from the gridviewrow
GridView gv = row.Parent.Parent as GridView;
//get the correct datakey from the gridview with the dataitemindex
int SKU = Convert.ToInt32(gv.DataKeys[row.DataItemIndex].Values[0]);
//display result
Label1.Text = SKU.ToString();
}

retrieve data key of selected row in gridview in asp.net

I have a button (item template) in any rows of a gridView.
I want when user clicks on this button, I retrieve dataKey of row that there is that button in it.
how can I do this?
(sorry,I can't speak and write English very well)
Assuming you have one DataKey and it is an int and the row of the button isn't necessarily selected and you have connected the following function to the buttons in your template field:
protected void RowClicked(object sender, EventArgs e)
{
Button TempButton = sender as Button;
if (TempButton != null)
{
GridViewRow GVR = TempButton.Parent.Parent as GridViewRow;
if (GVR != null)
{
int ID = (int) GridView1.DataKeys[GVR.DataItemIndex].Value;
}
}
}
Try the SelectedIndexChanged event. Something like this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
.
.
.
}

How to get the value of selected row using radiobutton in gridview in a button click

i need to get the value of the selected row of radiobutton in the gridview. the selected value is assigned to a textbox. my code is
protected void lnBTNDone_Click(object sender, EventArgs e)
{
GridViewRow gvRow = grdEventDetails.SelectedRow;
txtEventId.Text = gvRow.Cells[0].Text;
}
the problem is the gvRow value is assigned as null .
the linkbutton is not inside gridview. the gridview n linkbutton and the textbox are inside the user controls.
You can always get the GridViewRow via the sender's NamingContainer:
protected void lnBTNDone_Click(object sender, EventArgs e)
{
LinkButton lnBTNDone = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnBTNDone.NamingContainer;
txtEventId.Text = row.Cells[0].Text;
}
Assuming that the LinkButton is in the selected row.
Btw, i'm yet not sure why SelectedRow is null there, maybe because the GridView's SelectedIndexChanged event is triggered after the LinkButton's click event.
protected void lnBTNDone_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdEventDetails.Rows.Count; i++)
{
RadioButton rb = (grdEventDetails.Rows[i].FindControl("grdRdo")) as RadioButton;
if (rb.Checked == true)
{
txtEventId.Text = grdEventDetails.Rows[i].Cells[1].Text;
}
}
}

ASP.net GridView: hide Edit|Delete links

I have GridView with AutoGenerateDeleteButton=true && AutoGenerateEditButton=true.
I want to allow only registered users to use these functions therefore I want to hide it from unregistered users. How can I hide it?
I tried hidding the whole column but on page_load gridView is not ready yet so I get null exception.
On your pageLoad event store user Role inside Session
protected void Page_Load(object sender, EventArgs e)
{
Session["usrRole"] = "1";
}
On Row databound event of your gridview check for the session & if not equal to your administrator role, set visibility of your delete button column to false
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Session["usrRole"] != "1")
{
e.Row.Cells[0].Visible = false; //0 is autogenerate edit column index
e.Row.Cells[1].Visible = false; // 1 is autogenerate delete column index
}
}
}

Get the row that has been clicked in the gridview

I have added OnClick event dynamically for gridview.So when I click on any of the row on grid view, the event is fired but I can't get which row is clicked.
This is the code where I add event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnGrid, string.Empty);
}
//This is the code which catches this event
protected void GridView1_OnClick(object sender, EventArgs e)
{
_ID="10" //Static data. I need this from the gridview1.row[ClickedRow].cell[0]
}
I fear you'll have to "manually" assign this value.
For this, add hidden input element to the form:
<input type="hidden" name="clicked_row" />
Change the code to:
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnGrid, string.Empty) + "; document.forms[0].elements['clicked_row'].value = '" + e.Row.RowIndex + "';";
And finally, read the index from the Request in the GridView1_OnClick method:
_ID = Int32.Parse(Request.Form["clicked_row"]);
If each row has its own ID change e.Row.RowIndex to e.Row.Id.
I think, you could pass your id/rowindex as parameter to GetPostBackClientHyperlink() and implement the IPostBackEventHandler-interface in your page. The RaisePostBackEvent() method will get the id as input parameter passed in.
I haven't tested this, I just took a look at the example in MSDN documentation
Try passing in the row instead of the grid, like this:
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(e.Row, String.Empty);
And in your event handler:
protected void GridView1_OnClick(object sender, EventArgs e)
{
if (sender is GridViewRow)
{
int index = ((GridViewRow)sender).RowIndex;
}
}
I'm not positive, but I think the GridView OnClick will still fire since you're passing a child element of the grid.

Resources