I'm trying to take a GridView and get back the data from the row that was clicked. I've tried the code below and when I click the row I get back the selected index but when I look at the actual rows in the GridView they show empty. Not sure what I am missing.
.ASP make my grid.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
CssClass="datatables" Width="100%"
DataSourceID="SqlDataSource1"
GridLines="None" ShowFooter="True" AllowSorting="True"
onrowcreated="GridView1_RowCreated"
onrowdatabound="GridView1_RowDataBound" ShowHeaderWhenEmpty="True"
onrowcommand="GridView1_RowCommand"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<HeaderStyle CssClass="hdrow" />
<RowStyle CssClass="datarow" />
<PagerStyle CssClass="cssPager" />
</asp:GridView>
On each row data bound I make sure that the click should set the selected index.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
}
}
Then when the selected index changes by clicking this gets fired which I can put a breakpoint on the first line and I see the index of what I clicked on get stored in a. However when I get to the foreach it skips right past it because it shows GridView1 having a Count of 0 rows. In theory it should have a couple hundred rows and when the index matches it should grab the data in the 6th cell over and store it in string b. Why am I getting no rows on the click?
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int a = GridView1.SelectedIndex
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == a)
{
b = row.Cells[6].Text;
}
}
}
Here is my page load.
protected void Page_Load(object sender, EventArgs e)
{
c = HttpContext.Current.Session["c"].ToString();
SqlDataSource1.ConnectionString = //My secret
string strSelect = "SELECT columnnames from tablenames where c in (#c)
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectCommand = strSelect;
SqlDataSource1.SelectParameters.Add("c", c);
try
{
GridView1.DataBind();
}
catch (Exception e)
{
}
GridView1.AutoGenerateColumns = true;
}
Try just grabbing the row from the SelectedRow property:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string b = row.Cells[6].Text;
}
It's my understanding that the Rows collection doesn't get repopulated on PostBacks when you're using those data source controls (like SqlDataSource).
You could probably use your existing code if you called .DataBind() on your GridView prior to trying to iterate through the Rows:
GridView1.DataSourceID="SqlDataSource1";
GridView1.DataBind();
But that seems a little hacky.
After seeing your Page_Load, I see that you need to wrap your databinding code in an if(!Page.IsPostBack) block. Databinding on every postback is interrupting the process of ASP.NET maintaining the state of your controls via the ViewState.
Related
I am developing a Web Form, where I show Gridview with data. One of the column consists of CheckBox. How can I update data in particular row.
so my question is:
How to unidentified particular row and send an sql request with UPDATE when user Check or Uncheck the CheckBox?
Update:
Here is my code that i have. It doesn't update value of CheckBox.
namespace:
public partial class Call_Bills : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
string check;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Submit(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView2.Rows)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
con.Open();
bool private1 = (row.FindControl("CheckBox1") as CheckBox).Checked;
if (private1 == true)
{
check = "1";
}
else
{
check = "0";
}
SqlCommand cmd = new SqlCommand("insert into DetailCosts(private) values(#private)", con);
cmd.Parameters.AddWithValue("#private", check);
cmd.ExecuteNonQuery();
}}
name of GridView is: GridView2;
name of Checkbox in the Table: private;
id of CheckBox in Gridview is:
<asp:TemplateField HeaderText="Private" SortExpression="private">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("private") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
you can manually define a method to handle a postback generated by a control inside a gridview column. To do that you just need to add the tags AutoPostBack=True and OnCheckedChanged=YourMethodName to the control.
On the code-behind, define this method as public void, and define the parameters that usually would be there (sender as object and e as EventArgs), like:
public void YourMethodName(object sender, EventArgs e)
On the method, you may need to get the GridViewRow which contains the control that has generated the event. To do that, just parse the sender parameter (which will be the checkbox) and get 2 parent levels (GridViewCell and GridViewRow), it would be something like:
GridViewRow row = ((CheckBox)sender).parent.parent;
Since you have the row, you may get any control inside it with the FindControl method. If you need an id to identify the entitiy being updated, you may store it in a hidden field inside the row.
Hope it helps!
I have a GridView control that uses a datatable for a data source. I have enabled paging on the control, and the GridView shows the number of rows I've specified as the PageSize. However, there is no paging navigation, so I have no way to change the page.
Question: How do I get the paging navigation to show?
GridView definition:
<asp:GridView runat="server" ID="gvResults" CssClass="report" DataKeyNames="LogId" AllowSorting="True" AllowPaging="True" PageSize="5" OnRowDataBound="gvResults_OnRowDataBound" OnPageIndexChanging="gvResults_OnPageIndexChanging"></asp:GridView>
C#:
//...stuff that gets data from database
DataTable dt = oDs.DataSet.Tables[0];
gvResults.DataSource = dt;
gvResults.DataBind();
NOTE: I've verified in debugger that the datatable dt has more than 100 rows
And the OnPageIndexChanging() event, based on the answer to this question, although it didn't work in my case (I'm not sure why it's necessary since the event shouldn't trigger until you attempt to go to the next page, which would require the paging navigation to show in the first place, right?):
protected void gvResults_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
DataView dv = gv.DataSource as DataView;
if (dv != null)
{
DataTable dt = dv.Table;
gv.DataSource = dt;
}
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
Here is what I see:
I expect to see some way to page through the results.
I have also tried adding a PagerSettings to my GridView, like so:
<asp:GridView runat="server" ID="gvResults" CssClass="report" DataKeyNames="LogId" AllowSorting="True" AllowPaging="True" PageSize="5" OnRowDataBound="gvResults_OnRowDataBound" OnPageIndexChanging="gvResults_OnPageIndexChanging">
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Position="Bottom"></PagerSettings>
</asp:GridView>
Here is what the rendered HTML looks like for that last row. I modified the css during runtime to expand the row so it's clear that the row is empty. There are no paging controls being hidden by css.
After much banging of head and gnashing of teeth, I finally figured this out.
I have another event that operates OnRowDataBound() which formats a particular column to HTML, and this conversion resulted in no paging controls when it operated on the footer row.
The solution was to check that the bound row is a DataRow before doing the conversion.
protected void gvResults_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (!(sender is GridView) || e.Row.RowType != DataControlRowType.DataRow) return;
//do stuff
}
And then happiness ensued:
I have a datagridview which is loaded through a strored procedure. It has an ID column which i want to hide it but still access its value. I set the dataKeyNames and i have a method
protected void grdTime_OnDataBound(object sender,EventArgs e)
{
grdTime.Columns[1].Visible = false;
}
which i am trying to hide this column but i get the following error. If i remove the line inside the method it works. Here is the error.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Try this code in the method and then check..
grdTime.Columns[0].Visible = false;
hide the column in Row_Created event
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].CssClass = "hiddencol";
//e.Row.Cells[2].CssClass = "hiddencol";
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].CssClass = "hiddencol";
//e.Row.Cells[2].CssClass = "hiddencol";
}
}
here hiddencol is a css class
.hiddencol
{
display:none;
}
in the above code i'm hiding second column of my grid
If you've set GridView's AutoGenerateColumns to true(default), the Columns-Collection is empty. Following should work then:
1.)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
2.)
foreach (TableRow row in GridView1.Controls[0].Controls)
{
row.Cells[0].Visible = false;
}
you can use like this in gridview tag on .aspx page
<Columns>
<asp:BoundField HeaderText="Merchandise Id" DataField="MerchandiseId" Visible="false" /></columns>
I have an asp.net GridView:
<asp:TemplateField HeaderText="View Faktor" ShowHeader="False" Visible="True">
<ItemTemplate>
<asp:ImageButton ID="imgBtn1" CssClass="SelectRow" runat="server" CausesValidation="false"
CommandArgument='<%#(eval("mprID")) %>' CommandName="ViewFactors" ImageUrl="~/tadarokat/Images/factor.png"
Text="" />
</ItemTemplate>
</asp:TemplateField>
How Can I get rowIndex on row command event?
I want to highlight (select) target row when RowCommand fires.
this is answer for your question.
GridViewRow gvr = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
int RowIndex = gvr.RowIndex;
ImageButton \ Button etc.
CommandArgument='<%# Container.DataItemIndex%>'
code-behind
protected void gvProductsList_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = e.CommandArgument;
}
Or, you can use a control class instead of their types:
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int RowIndex = row.RowIndex;
If you have a built-in command of GridView like insert, update or delete, on row command you can use the following code to get the index:
int index = Convert.ToInt32(e.CommandArgument);
In a custom command, you can set the command argument to yourRow.RowIndex.ToString() and then get it back in the RowCommand event handler. Unless, of course, you need the command argument for another purpose.
I was able to use #rahularyansharma's answer above in my own project, with one minor modification. I needed to get the value of particular cells on the row on which the user clicks a LinkButton. The second line can be modified to get the value of as many cells as you wish.
Here is my solution:
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
string typecore = gvr.Cells[3].Text.ToString().Trim();
protected void gvProductsList_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Delete")
{
GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int RemoveAt = gvr.RowIndex;
DataTable dt = new DataTable();
dt = (DataTable)ViewState["Products"];
dt.Rows.RemoveAt(RemoveAt);
dt.AcceptChanges();
ViewState["Products"] = dt;
}
}
catch (Exception ex)
{
throw;
}
}
protected void gvProductsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
gvProductsList.DataSource = ViewState["Products"];
gvProductsList.DataBind();
}
catch (Exception ex)
{
}
}
I have a grid view displaying the messages a user has. Each message the user has is being marked whether it has been read or unread as a bit in my database table.
Is there a way how I can change the style of certain rows in my grid view according to whether the messages are read or unread? I wish to display the whole row with an unread message in bold.
You will need to use the RowDataBound event for such a task. Here is an example:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
...
</asp:GridView>
.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// searching through the rows
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool isnew = (bool)DataBinder.Eval(e.Row.DataItem, "IsNew");
if ( isnew ) e.Row.BackColor = Color.FromName("#FAF7DA"); // is a "new" row
}
}
Reference: http://blog.devexperience.net/en/5/Change_background_color_of_GridView's_Rows.aspx