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.
Related
I have grid view.there are two BoundField.here i want to set tooltip on BoundField DataField HeaderText Topic.
code.
<asp:GridView ID="Dgvlist" runat="server" >
<Columns>
<asp:BoundField DataField="topic" HeaderText="Topic" />
<asp:BoundField DataField="question" HeaderText="Question" />
</Columns>
</asp:GridView>
there are any solution?
There are 3 usual ways to set tooltip on BoundField column:
1) Using code-behind RowDataBound event
protected void Dgvlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Cells[0].ToolTip = DataBinder.Eval(e.Row.DataItem, "Topic", string.Empty);
}
}
2) Using code-behind RowCreated event
protected void Dgvlist_RowCreated(object sender, GridViewRowEventArgs e)
{
foreach (TableRow row in Dgvlist.Controls[0].Controls)
{
row.Cells[0].ToolTip = DataBinder.Eval(e.Row.DataItem, "Topic", string.Empty);
}
}
3) Convert to TemplateField and use Label control
<asp:GridView ID="Dgvlist" runat="server" ...>
<Columns>
<asp:TemplateField HeaderText="Topic">
<asp:Label ID="TopicID" runat="server" Text='<%# Eval("topic") %>' ToolTip='<%# Eval("topic") %>'>
</asp:Label>
</asp:TemplateField>
<asp:BoundField DataField="question" HeaderText="Question" />
</Columns>
</asp:GridView>
The actual implementation depends on what method you're using.
Related issue:
How to add tooltip to BoundField
One hacky way to achieve this is to convert your BoundField to TemplateField option.
Convert this:
<asp:BoundField DataField="topic" HeaderText="Topic" />
To this:
<asp:TemplateField HeaderText="Topic">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Topic") %>' ToolTip ='<%# Bind("Topic") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Or From code behind you can do it in RowDataBound event like this
protected void Dgvlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;
}
}
i have a grid view in Asp.net. i want to generate button for specific rows.
Like i have a column named as "Status". If status is "Accept" a button should be generated in that particular row.
column
You can use a TemplateField in ASP.NET GridView and conditionally set the Button visibility, like shown below:
<asp:GridView runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Accept"
Visible='<%# Eval("Status")=="Accept" %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Hope this will help.
first add template field button like this
<asp:TemplateField HeaderText="status" ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btn" runat="server" CausesValidation="false"
CommandName="Select" Text="button" />
</ItemTemplate>
</asp:TemplateField>
and codebehind
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)e.Row.FindControl("btn");
if (e.Row.Cells[1].Text == "Accept")//replace 1 by your column(status) index in gridview
{
btn.Visible = true;
}
else
{
btn.Visible = false;
}
}
}
and to add code to this button so add it on
protected void grid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//code
}
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));
}
I have a Gridview.I placed checkbox on each row.By selecting the checkbox and clicking on delete button i should get all the selected row of that gridview in the codebehind.Please do help me...
<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" style="margin-left: 58px; margin-top: 13px" >
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Delete" runat="server" Text="Button" runat="server"
onclick="Delete_Click" />
codebehind
protected void Button1_Click(object sender, EventArgs e)
{
string valueForDB = DatabaseList.SelectedValue;
Data obj = new Data();
if (valueForDB == "virtualworkplace")
{
obj.getDocforVirtualwrkspace(valueForDB);
GridView1.DataSource = obj.getDocforVirtualwrkspace(valueForDB);
GridView1.DataBind();
}
What to write here???
protected void Delete_Click(object sender, EventArgs e)
{
}
Try this
private void DeleteRows()
{
foreach (GridViewRow row in gridView1.Rows)
{
HtmlInputCheckBox chk = (HtmlInputCheckBox) row.Cells[0].FindControl("selectedrowchk");
if (chk != null && chk.Checked)
{
string id = gridView1.DataKeys[row.RowIndex].Value.ToString(); // get the record's ID of this row
deleteRecord(id);
}
}
//RefreshGrid();
}
This code is assuming you set the DataKeyNames property of the GridView to store the primary key of each record. It is also assuming you are putting the checkbox in the first column
to set datakeys use the datakeynames property from the gridview
<asp:GridView AllowSorting="true" AutoGenerateColumns="false" AllowPaging="true"
PageSize="10" DataKeyNames="MembershipNo"
ID="grdvw_showdetails" runat="server" CellPadding="4"
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;
}