I'm new to ASP.NET and I am using GridView to display data on a web page. I also added a button in Gridview called process orders.
<asp:GridView ID="GridView1" runat="server" autogenerateselectbutton="True" GridLines="None" AllowPaging ="true"
OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Process" runat="server"
CommandName="processorders"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Process orders" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I used the row command method to get the selected index of the row.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "processorders")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
}
Also I've implemented selectedIndexChanged method for the default select button:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
txtbox.Text = "You selected " + row.Cells[3].Text + ".";
}
If I try to use the GridViewRow in GridView1_RowCommand I get a null pointer exception. The row is always null but it works fine in GridView1_SelectedIndexChanged.
What am I trying to achieve is, get the column value from the selected row using my Process button and then use that value to update a database. I don't want to do it using Select.
And is there any way I can use row.Cells[] without specifying the index. I want to get the value of a column name.
Help? Please ??
Related
I have a gridview named gvappts.
This grid has seven columns one for each day of the week.
The grid also has multiple rows, each cell has a button inside.
If I am using an onRowCommand event is there a way to find the name of the button that was clicked?
I started off with this:
if (e.CommandName == "GetData")
{
//Get rowindex
int rowindex = Convert.ToInt32(e.CommandArgument);
//Get Row
GridViewRow gvr = gvappts.Rows[rowindex];
}
not sure if I am going about this quite right.
Here is a snippet for the gridview on the web page, all the columns are using TemplateFields.
<Columns>
<asp:TemplateField HeaderText="Day 1" HeaderStyle-CssClass= "hdr" ItemStyle-CssClass="Grid">
<ItemTemplate>
<asp:LinkButton id="lbd1" runat="server" Text='<%#(Eval("Day1"))%>' CommandName="GetData" CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can use the CommandSource and cast it back to a LinkButton.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lb = e.CommandSource as LinkButton;
string value = lb.CommandArgument;
}
how can i pass the value from the gridview to the next webform? here is my gridview code below:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" PostBackUrl='<%# "Details.aspx?RowIndex=" + Container.DataItemIndex %>'></asp:LinkButton>
</ItemTemplate
</asp:TemplateField>
but i can't seem to make it work. it goes to the next form but does not get the value. my id has a format "1000001" and it does read it.
here is my code for the next page. i created a new gridview just to store the value from.
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView gv1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
GridViewRow row = GridView1.Rows[rowIndex];
lblID.Text = row.Cells[0].Text;
Add OnRowCommand="GridView_RowCommand" like
<asp:GridView ID="GridView" OnRowCommand="GridView_RowCommand"
Update your Columns as
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" CommandName= "Redirect" CommandArgument='<%#Eval("ID")%>' ></asp:LinkButton>
</ItemTemplate
In Code-Behind file
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Redirect") {
//Get Command Argument
Response.Redirect("Details.aspx?RowIndex="+e.CommandArgument.ToString(),true);
}
}
I have a grid view with a list of data and a LinkButton to delete the row.
<asp:GridView ID="gridApartment" EmptyDataText="No Records Found" runat="server" AutoGenerateColumns="False" BorderWidth="0"
AllowPaging="true" ShowFooter="false" PageSize="15" Width="100%" OnPageIndexChanging="gridApartment_PageIndexChanging"
CssClass="mGrid"
OnRowDeleting="gridApartment_RowDeleting" OnRowCommand="gridApartment_RowCommand">
<AlternatingRowStyle CssClass="alt" />
<PagerStyle CssClass="pgr" />
<Columns>
<asp:TemplateField HeaderText="Building">
<ItemTemplate>
<asp:Label ID="BuildingName" runat="server" Text='<%#Eval("BuildingName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="25%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" CssClass="aDelete" ToolTip="Delete" runat="server" CommandName="delete" OnClientClick=' javascript:return confirm("Are you sure you want to delete?"); '
CommandArgument='<%# Eval("RoomDetailsId") %>'>Delete</asp:LinkButton>
</ItemTemplate>
<HeaderStyle Width="8%"></HeaderStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
onload data filled by this code
gridApartment.DataSource = masterManager.GetAllRooms();
gridApartment.DataBind();
now i have a search box, and when i search, the filters result will get bind by this code.
gridApartment.DataSource = conobj.GetSearchDetails("usp_RoomDetailsSearch", "#SearchName", txtSearchterm.Text.Trim());
gridApartment.DataBind();
for delete the code is
protected void gridApartment_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
var masterManager = new MasterEntryManager();
int res = masterManager.DeletRoom(e.CommandArgument.ToString());
Search();
}
}
when the first time the grid get loaded e.CommandArgument has the right ID and i can delete the right row. But when i search and re bind the gridview, the e.CommandArgument value is not updated with new ID. It still return the same ID which is loaded first on page load.
For eg:
when the grid is first loaded, when i try delete the first row, e.CommandArgument has an ID say, 1001 and I deleted the record with ID 1001.
Now I load the gridview second time. Now the 1st row linkbutton e.CommandArgument has ID 1500.
I perform a search and the result grid has only 5 rows
Now i try to delete the first row, the expected linkbutton e.CommandArgument ID is say 2001, but i get the linkbutton e.CommandArgument ID as 1500, the value of first row ID when the gridview first loaded
Id is not get updated.
How can I get the updated e.CommandArgument value.?
Counter to expectations, the CommandArgument is not associated with the control that does the post back. Rather, the DataSource is returned as part of the ViewState, and the CommandArgument is referenced by the index of the control. If either the GridView or DataSource change, the index will no longer be correct.
You need to make sure that the GridView and DataSource are not updated prior referencing the CommandArgument, but then updated after the delete (it looks like you have this part). Remember that the post back event occurs after the page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridApartment.DataSource = masterManager.GetAllRooms();
gridApartment.DataBind();
}
}
I am trying to fetch data from certain GridView row when I click link in that row...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" AllowPaging="True">
<Columns>
<asp:BoundField DataField="nazivTeme" HeaderText="nazivTeme"
SortExpression="nazivTeme" />
<asp:BoundField DataField="datum" HeaderText="datum" SortExpression="datum" />
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkView" commandname="view"
OnClick="lnkView_Click">Komentiraj</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I would like to get data from the row where I click the LinkButton (only first cell).
I hope you understand what I want :)
Do not subscribe to Click event of the button. Instead, subscribe to the RowCommand event of the grid view. Then, in the event handler, you can evaluate which command was received and which row was affected. Once you get the row, you can get its cell value by cell index or ID:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "view")
{
// Retrieve the row index
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row by its index
GridViewRow row = this.GridView1.Rows[index];
// Get the 1st cell value from the row
string cellValue = r.Cells[0].Text;
}
}
I have a GridView which I bind to a SqlDataReader on Page_Load. It has a column with buttons and I am trying to get the row when a button is clicked with the following code:
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
EDIT : Pasting the .aspx page from the comments sectio
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" OnRowCommand="GridView1_RowCommand" DataKeyNames="id" GridLines="None"> <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</aspx:GridView>
I get the following error: 'System.FormatException: Input string was not in a correct format.' on line 'int index = Convert.ToInt32(e.CommandArgument);'.
Any ideas?
You need to check which command in the GridView row has been clicked. Your markup should correspondingly map. See egs below.
The e.CommandArgument you are getting may not correspond to your button click.
In CodeBehind:
void GridView1_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=="Add")
{
// 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 = CustomersGridView.Rows[index];
// additional logic...
}
// additional logic...
}
In Markup:
Also please ensure you have set your CommandArgument attribute appropriately. Example below:
<asp:Button (...) CommandArgument="<%# Container.DataItemIndex %>" />
OR use a buttonfield
<asp:ButtonField ButtonType="button" CommandName="Add" Text="Add" />
Can u post the whole markup code,it would be helpful to solve. according to your question
in gridview aspx code you have to use Command Name, and command Argument for the Button Control and it should bind to the one of the column of db. and use Row Command event of gridview. And also Try to use ItemTemplate to put Control Inside the gridview.
Click here for MSDN Documentation. Row Command in GridView
protected void Grid_RowCommand( object sender, GridViewCommandEventArgs e )
{
int index = Convert.ToInt32( e.CommandArgument );
your logic .......
}
You have not added value to the command Argument. For your Button event in .aspx page
<asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" CommandArgument = 1 Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' />
In the code behind i.e the RowCommand Event
if(e.CommandName == "Test")
{
int index = Convert.ToInt32(e.CommandArgument);
}
This will work only for value 1. To make it generic you can bind the command Argument to the value you want using one of the binding techniques for example : CommandArgument ='<%# Eval("ID") %>' (Assuming ID is present in the GridView)
Check out this code
void ContactsGridView_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=="Add")
{
// 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 = ContactsGridView.Rows[index];
// Create a new ListItem object for the contact in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
Server.HtmlDecode(row.Cells[3].Text);
// If the contact is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!ContactsListBox.Items.Contains(item))
{
ContactsListBox.Items.Add(item);
}
}
}
Below html code for gridview
<asp:gridview id="ContactsGridView"
datasourceid="ContactsSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="ContactsGridView_RowCommand"
runat="server">
<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="ContactID"
headertext="Contact ID"/>
<asp:boundfield datafield="FirstName"
headertext="First Name"/>
<asp:boundfield datafield="LastName"
headertext="Last Name"/>
</columns>
</asp:gridview>
Check link Gridview commands
Hope this answer helps you.