Asp.net GridView - Getting row identifier - asp.net

I have a page that displays orders in GridView and each row has a details- and delete-button. Most of the time everything works fine, but if new order arrives between the page refresh and button clicking, wrong order is processed.
I save unique identifier to CommandArgument, but it seems that the identifier in question isn't passed back to server, but instead some kind of counter (e.g. 5th order) and since after the arrival of new order the right one would be 6th, everything goes wrong... What's up with that?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="20"
EnableViewState="False" EnableSortingAndPagingCallbacks="True"
DataSourceID="ldsOrders" AllowPaging="True"
ondatabound="gvOrders_DataBound" CssClass="Gridview" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" CommandArgument='<%#Eval("ID") %>' CommandName="Delete"
Text="Delete" OnClick="btnDelete_Click" CausesValidation="False" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnOrder" runat="server" CommandArgument='<%#Eval("ID") %>' CommandName="Order"
Text="Order" OnCommand="btnOrder_Click" CausesValidation="False" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-Behind:
protected void btnDelete_Click(object sender, CommandEventArgs e)
{
string orderID = (string)e.CommandArgument;
da.DeleteOrder(Convert.ToInt32(orderID));
Response.Redirect(Request.RawUrl, false);
}

Without any code examples here is something to try...
Assuming that ID is the primary key - you should set that in the GridView DataKeys property. You should not place primary keys in a viewable area.
Where you specify the CommandArgument: that should be the rowindex that is set in the RowDataBound event such that:
//(note this is pseudo-code, but you should get the basic idea)
if( e.row.rowtype == datarow )
{
Button btn = (Button) e.row.findcontrol("btnDelete") ;
btn.CommandArgument = e.row.rowIndex ;
}
And in the delete click event obtain the primary key from the gridview datakeys:
protected void btnDelete_Click(object sender, CommandEventArgs e)
{
int orderIDIdx = (Convert.ToInt32(e.CommandArgument);
int pkey = Convert.ToInt32(Gridview1.Datakeys(orderIDidx).value
da.DeleteOrder(pkey);
Response.Redirect(Request.RawUrl, false);
}
Obviously, add appropriate error checking where applicable.

You should set the DataKeyNames
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
<asp:GridView ID="Gridview1" DataKeyNames="ID" .... >
then the command delete trigger the RowDeleting event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx
which can be used to delete the row using e.Keys[0] to retrieve the id
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Int orderID = Convert.ToInt32(e.Keys[0].ToString());
da.DeleteOrder(Convert.ToInt32(orderID));
}

Related

Gridview.row.count show zero whenever button is click

i have a gridview ,whenever user will click on row of gridview view ,
record will be populated in the textbox ,but i am getting error as
index was out of range. must be non-negative and less than the size of
the collection
also i check gridview.row.count is showing zero.
please help below is my code
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" ShowHeader="false" AllowPaging="false" AllowSorting="false" ToolTip="Click Here To Edit"
Style="table-layout: fixed;" OnRowDataBound="GridView1_RowDataBound"
CssClass="mGrid" PagerStyle-CssClass="pgr" DataKeyNames="AcheadID"
AlternatingRowStyle-CssClass="alt" Width="100%" Height="100%" >
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField ItemStyle-Width="40px">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Account Head ID" DataField="AcheadID" HeaderStyle-HorizontalAlign="Left" Visible="false" />
<asp:TemplateField HeaderText="Account Head" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="400px">
<ItemTemplate>
<asp:Label ID="lblac_head" runat="server" Text='<%# Bind("ac_head") %>'></asp:Label>
<asp:HiddenField ID="hndacheadID" runat="server" Value='<%# Bind("AcheadID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnTest" runat="server" OnClick="GridView1_OnClick" style="display:none;" />
my codebehind as
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAccountHead();
}
}
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hndac_headid = (HiddenField)e.Row.FindControl("hndac_headid");
if (hndac_headid.Value != "0")
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d3d3d3'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
//e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnTest, e.Row.RowIndex.ToString());
}
}
}
protected void GridView1_OnClick(object sender, EventArgs e)
{
Button buttonSender = sender as Button;
int index;
GridViewRow row = null;
if (int.TryParse(Request.Params.Get("__EVENTARGUMENT"), out index))
{
row = GridView1.Rows[index];
Label ac_head = (Label)GridView1.Rows[index].FindControl("lblac_head");
}
Index out of range issue happens because Request.Params.Get("__EVENTARGUMENT") doesn't works for Button ( and addiionally ImageButton ) controls.
The Button controls and ImageButton controls does not call the __doPostBack function. Because of this, the _EVENTARGUMENT ( and _EVENTTARGET as well ) will always be empty.
However, other controls such as CheckBoxes, DropDownLists, LinkButtons uses javascript function __doPostBack to trigger a postback.
Try using a LinkButton
As a second check( can be ignored ), just make sure Data is binded to GridView properly like check the IsPostback conditions etc...
Check your GridView, it has an Id GridView2 but you always referencer GridView1.

Showing url in BoundField in gridview

I am developing a chatting web application with the help of the gridview....The problem is when the user enter the link and it is saved in the database and when the data is retrieved in the gridview the link should be click able. The field I am using to retrieve the data is BoundField.
I don't want to use hyperlink field because then in every row there will a link. I only want to display link when a user will enter a link.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" CssClass="amit1" >
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="m1" HeaderText="Message" SortExpression="m1" >
<HeaderStyle Font-Size="Larger" HorizontalAlign="Left" Width="280px" />
<ItemStyle Font-Size="Medium" HorizontalAlign="Left" Width="280px" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/gold.accdb"
SelectCommand="SELECT [m1] FROM [Chat_detail] WHERE ([Chat_id] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="Chat_id" QueryStringField="chat_id"
Type="String" />
</SelectParameters>
</asp:AccessDataSource>
Is there is any way to do this???
Thanks in advance.
Add Hyperlink, make use of RowDataBound event to make that hyperlink visible tru/false based on the value.
See Below:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hlnk = (HyperLink)e.Row.FindControl("hlnkUserEnetredLink");
if(!String.IsNullOrEmpty(hlnk))
{
hlnk.Visible = true;
}
else
{
hlnk.Visible = false;
}
}
}
Add that Hyperlink in your aspx page -- in Gridview..
You could disable the hyperlink in your code behind based on whatever condition. Note: Making visible = true/false will leave a empty cell in the gridview column.
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// find the HyperLink defined in the fridview definition
HyperLink myHyperLink = (HyperLink)e.Row.FindControl("hpLinkUrl");
//userstoredlink is the datasource field that holds the user stored link
string userLink = DataBinder.Eval(e.Row.DataItem, "userstoredlink").ToString();
if (string.IsNullOrEmpty(userLink))
{
// disable link
myHyperLink.Enabled = false;
}
}
}
Please refer my another post on different ways to add hyperlink to gridview
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="hpLinkUrl" runat="server" NavigateUrl='<%# Eval("userstoredlink", "{0}") %>' Text="View Details" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
Also remember you need to define OnRowDataBound="GridView1_RowDataBound" for your gridview.
Or if you don't want to display the ViewDetails text when there is no value for userstoredlink then changes the hyperlink definition as follows
<asp:HyperLink ID="hpLinkUrl" runat="server" NavigateUrl='<%# Eval("userstoredlink", "{0}") %>' Text='<%# Eval("userstoredlink") != "" ? "View Details" : "" %>' />

GridView DataBind does not work

I am using ASP.NET Membership and Linq. I have a problem here: I show all users inside a grid view that has a delete button. Take a look at this code:
<asp:GridView
ID="UsersGridView"
runat="server"
AutoGenerateColumns="False"
DataSourceID="UsersLinqDataSource"
AllowPaging="True">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
<asp:BoundField DataField="LastActivityDate" HeaderText="LastActivityDate" ReadOnly="True" SortExpression="LastActivityDate" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="DeleteButton" runat="server" CommandArgument='<%# Eval("UserName") %>' Text="Delete" OnClick="DeleteButton_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource
ID="UsersLinqDataSource"
runat="server"
ContextTypeName="TraceWeb.DataModel.DataContextDataContext"
EntityTypeName=""
Select="new (UserName, LastActivityDate)"
TableName="Users"
EnableDelete="True">
</asp:LinqDataSource>
And Delete button's event handler:
protected void DeleteButton_Click(object sender, EventArgs e)
{
String username = (String)((sender as IButtonControl).CommandArgument);
Membership.DeleteUser(username, true);
UsersGridView.DataBind();
}
But problem is that after running this code and deleting a user, GridView still shows that user.
This happens because Membership and UsersLinqDataSource are not connected and if you "refresh" the UsersLinqDataSource status and then rebind your grid, all will be displayed properly.
protected void DeleteButton_Click(object sender, EventArgs e)
{
String username = (String)((sender as IButtonControl).CommandArgument);
Membership.DeleteUser(username, true);
// first solution: may not work properly
UsersLinqDataSource = yourLinqData;
// second solution: work
UsersLinqDataSource = null;
UsersLinqDataSource = yourLinqData;
UsersGridView.DataBind();
}

show data in outside textbox from selected row in gridview

I am having a grid view created by using template fields. I inserted a link button using template fields in the grid view.
There are 4 textboxes outside the GridView.. i want to select the row on link button's click and put the selected row's data in text boxes. I am using a row command even for this but its not working ... the syntax i am using is .:
<asp:GridView ID="gview" AutoGenerateColumns="False" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" onrowcommand="gview_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Book Name">
<ItemTemplate>
<%#Eval("book_name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Author">
<ItemTemplate>
<%#Eval("book_author") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Publisher">
<ItemTemplate>
<%#Eval("book_Publisher") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Price">
<ItemTemplate>
<%#Eval("book_Price") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update">
<ItemTemplate>
<asp:LinkButton ID="lnkDet" CommandName="cmdBind" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" CausesValidation="false">View Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
and Code behind file :
protected void gview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdBind")
{
LinkButton lb = (LinkButton)e.CommandSource;
int index = Convert.ToInt32(lb.CommandArgument);
//Bind values in the text box of the pop up control
txt_name.Text = gview.Rows[index].Cells[0].Text;
txt_author.Text = gview.Rows[index].Cells[1].Text;
txt_price.Text = gview.Rows[index].Cells[2].Text;
}
}
Can anyone tell me how to do this.
I think this works:
protected void gview_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "cmdBind")
{
int index = Convert.ToInt32(e.CommandArgument);
//Bind values in the text box of the pop up control
txt_name.Text = gview.Rows[index].Cells[0].Text;
txt_author.Text = gview.Rows[index].Cells[1].Text;
txt_price.Text = gview.Rows[index].Cells[2].Text;
}}
Attempting it the way you are is not impossible, but it may be easier to just use the in built select buttons for each row. That way you can just use the SelectedIndexChanged event of the gridview:
protected void gview_SelectedIndexChanged(object sender, EventArgs e)
{
txt_name.Text = gview.SelectedRow.Cells[0].Text;
txt_author.Text = gview.SelectedRow.Cells[1].Text;
txt_price.Text = gview.SelectedRow.Cells[2].Text;
}

ASP.NET Grid View Problem

I have one gridview where I am passing the command argument as gridview row id for the Button I created for every row.
I want to display all the details of that row in the textbox according to the Row clicked.
<asp:GridView ID="gvCategory" runat="server" AutoGenerateColumns="false"
onrowcommand="gvCategory_RowCommand" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCatId" runat="server" Text='<%#Eval("categoryId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCatName" runat="server" Text='<%#Eval("categoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnModify" runat="server" Text="Modify" CommandName="Modify" CommandArgument='<%#Eval("categoryId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code.....
if (e.CommandName == "Modify")
{
int id = Convert.ToInt32(e.CommandArgument);
// I want the value to assgin for the selected row here
// I was previously fetching the data from database according id,but i want this frim the gridview of selected row.
}
I wrote a quick example of how to do what you're trying to do. It works for me.
The Example Solution
Default.aspx
<asp:GridView ID="myGridView" runat="server"
AutoGenerateColumns="False"
DataSourceID="StudentsDS"
DataKeyNames="ID"
OnRowCommand="myGridView_RowCommand"
OnSelectedIndexChanged="myGridView_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="FullName" HeaderText="FullName"
SortExpression="FullName" />
<asp:BoundField DataField="ClassID" HeaderText="ClassID"
SortExpression="ClassID" />
<asp:CommandField ShowSelectButton="True" SelectText="Modify" />
</Columns>
</asp:GridView>
<asp:TextBox ID="txtStudent" runat="server" />
<asp:SqlDataSource ID="StudentsDS" runat="server"
ConnectionString="<%$ ConnectionStrings:Sandbox %>"
SelectCommand="SELECT * FROM Student"
/>
Default.aspx.cs
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "Select") {
// do something here if you want, although not necessary
}
}
protected void myGridView_SelectedIndexChanged(object sender, EventArgs e) {
// show "FullName" field of selected row in textbox
txtStudent.Text = myGridView.SelectedRow.Cells[1].Text;
}
How It Works
Upon clicking "Modify" in a row, the textbox updates to show the FullName field of the selected row.
The important part is that instead of a TemplateField, use a CommandField with ShowSelectButton="True". Then do what you need to do in the SelectedIndexChanged event handler. Note that the SelectText of the CommandField is set to "Modify" as you desired. You can also set the ButtonType property of the CommandField to be button, image, or link.
Making It Better
I would also advise that instead of using a SqlDataSource as I have, you use an ObjectDataSource so that you can do something like
protected void myGridView_SelectedIndexChanged(object sender, EventArgs e) {
MyModelObject o = myGridView.SelectedRow.DataItem as MyModelObject;
txtStudent.Text = o.MyProperty;
}
You may also consider wrapping your GridView in an UpdatePanel to prevent full postbacks / page refreshes.
After you have the lineID, you select that line, and then you have the CategoryID on selected value
int iTheIndexNow = Convert.ToInt32(e.CommandArgument);
// select that line to see it visual as selected, and get the value on next line
gvCategory.SelectedIndex = iTheIndexNow;
// here is your categoryID that
//you can use to open your database with and get the text
gvCategory.SelectedValue // == categoryID
On GridView you must have set your KeyID
<asp:GridView DataKeyNames="categoryId" .... >

Resources