I have an issue where the pager number doesn't get updated automatically when I hide rows in the gridview. Do I need to set the pager value manually? Can someone suggest me on this?
ASPX page
<asp:GridView ID="SearchResults" runat="Server" AutoGenerateColumns="false"
EnableViewState="false" AllowPaging="true" PageSize="50"
OnDataBound ="SearchResults_DataBound"
OnRowDataBound="SearchResults_RowDataBound">
<RowStyle CssClass="EvenRow" />
<AlternatingRowStyle CssClass="OddRow" />
<Columns>
<asp:TemplateField meta:resourceKey="UmSellField">
<ItemStyle CssClass="alpha" />
<HeaderStyle CssClass="alpha" />
<ItemTemplate>
<asp:Label ID="UmSellLabel" runat="server" EnableViewState="false"
Text='<%# GetUnitOfMeasure(Container.DataItem,false) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
codebehind
protected void SearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
e.Row.Visible = showRow;
e.Row.Cells[0].Visible = showRow;
}
}
ShowRow is a boolean value that gets set in GetUnitOfMeasure function (not copied here) based on these conditions.
Related
Dear all genius please help me with this had been trying everywhere but no luck. I am trying to open my nested gridview for a single Parent Row but gets open the entire parent row with one single record of the child row. It should be when I click the button of the Parent gridview row then the record for the particular row should display in the Child gridview in the same Parent row, but in my case the record of the Parent row shows up and the Child gridview gets open in all the parent row. Please need in help :(
enter image description here
<asp:GridView runat="server" ID="GridView4" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="UId" class="" CellPadding="4" PageSize="5">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="File Type" Visible="false">
<ItemTemplate>
<asp:Label ID="lblFileType" runat="server" Text='<%# Eval("FileType") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image Name" Visible="false">
<ItemTemplate>
<asp:Label ID="lblImageName" runat="server" Text='<%# Eval("ImageName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="Image2" class="img1" runat="server" ImageUrl='<%# Bind("pic") %>' OnCommand="blah_Command" CommandName='<%# Eval("UId") %>' CommandArgument='<%# Eval("pic") %>' />
<asp:Panel ID="pnlOrders" runat="server" Style="position: relative" Visible="false">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="FileSize" HeaderText="Order Id" />
<asp:BoundField ItemStyle-Width="150px" DataField="ShareByUserId" HeaderText="Date" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#ffffff" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#ffffff" Font-Bold="True" ForeColor="White" />
</asp:GridView>
Code Behind:
protected void blah_Command(object sender, CommandEventArgs e)
{
foreach (GridViewRow row2 in GridView4.Rows)
{
row2.FindControl("pnlOrders").Visible = true;
string customerId = e.CommandName.ToString();
GridView gvOrders = row2.FindControl("gvOrders") as GridView;
BindOrders(customerId, gvOrders);
}
}
private void BindOrders(string customerId, GridView gvOrders)
{
gvOrders.ToolTip = customerId;
gvOrders.DataSource = GetData(string.Format("select * from SShare where UId='{0}'", customerId));
gvOrders.DataBind();
}
You are looping all the rows in GridView4, so every nested GridView will be filled with data.
What you need is the row number of the row that the clicked LinkButton is in, and use that to find the nested GridView.
protected void blah_Command(object sender, CommandEventArgs e)
{
//get the current datagrid item from the sender
GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
//the row index of the gridviewrow
int rowIndex = row.RowIndex;
//find the correct nested grid using the row index
GridView gvOrders = GridView4.Rows[rowIndex].FindControl("gvOrders") as GridView;
}
I have a GridView that has data bounded rows. I'm trying to get specific cell value on the SelectedIndexChanged event of DropDownList. My tries are as follows:
string temp= GridView2.SelectedRow.Cells[3].Text;
string temp = ((DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0]).Text;
DataBoundLiteralControl dblc = (DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0];
string temp=dblc.Text;
These all 3 of them returns null.
Moreover, the Control[0] is returning correct value of TemplateFields only, but not DataBound fields.
.aspx
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #d54d7b" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" Width="900px" Font-Names="Segoe UI Light" BorderColor="#DEDEDE">
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="80" />
<asp:BoundField DataField="Vacancies" HeaderText="Vacancies" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="80" />
<asp:BoundField DataField="Time" HeaderText="Time" ItemStyle-Width="80" />
</Columns>
Problem you are probably facing is that, GridView1 hasn't been bounded yet.. that is the reason why Control[0] is returning correct value of TemplateFields but cells in the gridview returns null.
You can do the following
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
/// send value to filter or bind gridView1
/// bind gridvew
string temp = GridView1.Rows[0].Cells[3].Text;
/// or any code to get values from GridView Cell
}
I Converted all <asp:BoundFields> to <asp:TemplateField> and it looked like this:
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #009C0D" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" CssClass="test"
HtmlEncode="true"
Font-Names="Segoe UI Light" BorderColor="#DEDEDE" >
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:TemplateField HeaderText="Description" ControlStyle-Width="250px" ><ItemTemplate> <asp:LinkButton ID= "Description" PostBackUrl='<%# Eval("Description", "~/{0}.aspx") %>' Text='<%# Eval("Description")%>' runat="server" ></asp:LinkButton> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Vacancies"><ItemTemplate> <asp:Label ID="Vacancies" Text='<%# Eval("Vacancies")%>' runat="server" ></asp:Label> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Detail"><ItemTemplate><asp:Label ID="City" Text='<%# Eval("City")%>' runat="server" ></asp:Label> <div style="font-size:10px"> <asp:Label ID="Label1" Text='<%# Eval("DateDay")%>' runat="server" ></asp:Label> </div> </ItemTemplate></asp:TemplateField>
//and others
</Columns>
</asp:GridView>
And get specific value by entering this code behind DropDownList:
if (dropdown.SelectedIndex != -1)
{
ListItem mySelectedItem = (from ListItem li in dropdown.Items where li.Selected == true select li).First();
foreach (GridViewRow rw in GridView2.Rows)
{
Label tv = (Label)rw.Cells[3].FindControl("City");
if (tv.Text.IndexOf(mySelectedItem.Text) != -1)
{
rw.Visible = true;
}
else
rw.Visible = false;
}
}
Can't believe I have to ask this - you'd think such a basic feature would be straightforward to implement, but I'm having trouble creating a footer for a Gridview. I've checked out various tutorials and other questions, such as here, and here and here, but am still encountering difficulties.
The problem is in displaying the footer properly (i.e. without adding an extra empty column). From what I gather, you need to put the FooterTemplate inside a TemplateField tag or it won't work - at least it wouldn't compile for me. If I insert this after the BoundFields columns then it adds an extra column which is not desirable.
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AllowSorting="true"
CellPadding="3" HorizontalAlign="Center" GridLines="both" CssClass="dataTable1"
OnRowDataBound="Colour_Columns" Caption="PARTIAL COMPARE" ShowFooter="true">
<HeaderStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="header" />
<FooterStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="footer" />
<Columns>
<asp:BoundField DataField="FOLDER" HeaderText="Location" />
<asp:BoundField DataField="FILE" HeaderText="File" />
<asp:BoundField DataField="CHECKSUM" HeaderText="Checksum" Visible="false" />
<asp:BoundField DataField="STATUS" HeaderText="Status" />
<asp:BoundField DataField="DATE" HeaderText="Date" Visible="false" />
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="UpdateButton" runat="server" Text="UPDATE" CssClass="updateButton" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Similarly, if I put it before the BoundFields it adds an extra column on the left. If I try to put all the BoundFields under the TemplateField it won't compile.
How do I add the footer to the gridview without creating an extra column? Also, while we're at it, how can I set its colspan to 1? (It's only going to have the one Update button in it, so no need for three columns in the footer.)
Colour-Scheme method:
protected void Colour_Columns(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text == "Match")
e.Row.BackColor = Color.Lime;
if (e.Row.Cells[3].Text == "Mismatch")
e.Row.BackColor = Color.Gold;
if (e.Row.Cells[3].Text == "New File")
e.Row.BackColor = Color.PeachPuff;
}
}
This method does not seem to recognize ItemTemplate values...
Try using template field only for the last column and in that column you can specify the ItemTemplate and FooterTemplate. Try the code below.
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AllowSorting="true"
CellPadding="3" HorizontalAlign="Center" GridLines="both" CssClass="dataTable1"
OnRowDataBound="Colour_Columns" Caption="PARTIAL COMPARE" ShowFooter="true">
<HeaderStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="header" />
<FooterStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="footer" />
<Columns>
<asp:BoundField DataField="FOLDER" HeaderText="Location" />
<asp:BoundField DataField="FILE" HeaderText="File" />
<asp:BoundField DataField="CHECKSUM" HeaderText="Checksum" Visible="false" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("STATUS") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="UpdateButton" runat="server" Text="UPDATE" CssClass="updateButton" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DATE" HeaderText="Date" Visible="false" />
</Columns>
</asp:GridView>
I changed the Cs file to read the value from the template field. Please recopy the ASPX to becouse I changed it too by adding an ID to the Label
CS:
protected void Colour_Columns(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label StatusLabel = e.Row.FindControl("StatusLabel") as Label;
if (StatusLabel.Text == "Match")
e.Row.BackColor = Color.Lime;
if (StatusLabel.Text == "Mismatch")
e.Row.BackColor = Color.Gold;
if (StatusLabel.Text == "New File")
e.Row.BackColor = Color.PeachPuff;
}
}
You shouldn't use FooterTemplate with BoundField. Footer templates are meant to be used in conjunction with TemplateField.Additionally footer templates are applied per column this is done so you can aggregate totals at the bottom of the gridview in cases where you have numeric data.
Here's an example of using template fields with a footer field in the first column, you can change this as required to suit your needs:
ASPX:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFolder" runat="server" Text='<%# Eval("FOLDER") %>' />
</ItemTemplate>
<FooterTemplate>
Footer content displayed under FOLDER, notice no extra column!
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFile" runat="server" Text='<%# Eval("FILE") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCheck" runat="server" Text='<%# Eval("CHECKSUM") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Result:
Alternatively, you can stick with using BoundFields and add the footer dynamically in code:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
var footer = new Label();
footer.Text = "Footer content";
//Footer will be displayed under the *first* column
e.Row.Cells[0].Controls.Add(footer);
}
}
How to code this form:
PURCHASE ORDER MASTER
[lblPONumberH]
PO Number ://here is textBox //RequireField
PO Date : //here istextbox with Calender extender
Vendor: //here is dropdown
Created By ://here is textbox
save//Button Cancel//Button
GridView
Item Description Budget Number Quantity UOM Price
Databound DataBound DataBound DataBound DataBound Edit & Del btn
in footer style
txtItemDescription ddlBnumber txtQuantity ddlUOM txtPrice AddBtn
I have 5 check box on my page and a Grid view with Template Field, I am not using any bond field on page load I am binding the grid with all data of a table, I want to filter the data based on the check box check.
suppose: I have check box like A B C D. All check box are out side of Grid view. When user checked the check box A, then in Grid view, check box A related data should show, like wise for B C and D.
how do that?, Some one please give some example code and bit logic.
It would be great if I'll be able to filter the gridview without any postback.
Grid:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" CellPadding="3">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discription" SortExpression="Discription">
<ItemTemplate>
<asp:Label ID="lblDiscription" runat="server" Text='<%#Eval("Discription") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="Address">
<ItemTemplate>
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Binding the Grid:
TestPageDao page1Dao = new TestPageDao ();
if (!IsPostBack)
{
IList<TestDAO> TestDAO = page1Dao.GetAlldata();
GridView1.DataSource = TestDAO;
GridView1.DataBind();
}
I tried Filter gridview or http://forums.asp.net/p/1034014/2904713.aspx
If you want to avoid postback, uses jQuery like here:
http://jquerybyexample.blogspot.com/2012/04/how-to-filter-gridview-records-using.html
An alternative is AJAX, but at the end of the story it is a postback-like approach.
Finally using postback you can do it in several ways by dynamically setting the GridView filter before the GridView loads. This can be achieved with the Page load event, the OnSelecting of the associated datasource (if any), or similar.
Here it is an extraction of the aspx:
<asp:CheckBox ID="CheckBox1" runat="server" Text="A" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="B" />
<asp:CheckBox ID="CheckBox3" runat="server" Text="C" />
<asp:CheckBox ID="CheckBox4" runat="server" Text="D" />
<hr />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
DataSourceID="sqlDataSourceGridView" AutoGenerateColumns="False"
CssClass="GridViewStyle" GridLines="None" Width="650px" >
<Columns>
<asp:BoundField DataField="CompanyName" HeaderText="Company" ItemStyle-Width="200px" />
<asp:BoundField DataField="ContactName" HeaderText="Name" ItemStyle-Width="125px"/>
<asp:BoundField DataField="City" HeaderText="city" ItemStyle-Width="125px" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="125px" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceGridView" runat="server"
ConnectionString="<%$ ConnectionStrings:northWindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [City], [Country] FROM [Customers]"
OnSelecting="SqlDataSourceGridView_Selecting">
<FilterParameters>
<asp:ControlParameter ControlID="checkbox1" Name="CompanyName" PropertyName="Checked" ConvertEmptyStringToNull="false" />
</FilterParameters>
</asp:SqlDataSource>
Note the OnSelecting event and note that there isn't any filter preset.
Now in the code behind set dynamically the filter:
protected void SqlDataSourceGridView_Selecting(object sender, SqlDataSourceSelectingEventArgs e) {
SqlDataSourceGridView.FilterExpression = string.Empty;
if (CheckBox1.Checked) {
SqlDataSourceGridView.FilterExpression += "(CompanyName=1)";
}
if (CheckBox2.Checked) {
if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR ";
SqlDataSourceGridView.FilterExpression += "(B=1)";
}
if (CheckBox3.Checked) {
if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR ";
SqlDataSourceGridView.FilterExpression += "(C=1)";
}
if (CheckBox4.Checked) {
if (!string.IsNullOrEmpty(SqlDataSourceGridView.FilterExpression)) SqlDataSourceGridView.FilterExpression += " OR ";
SqlDataSourceGridView.FilterExpression += "(D=1)";
}
}
If you don't like the OnSelecting event, you can do the same in the PageLoad:
protected void Page_Load(object sender, EventArgs e) {
// here same code of above
// . . .
}
I didn't test it so verify minor errors.
I have a gridview like this :
<asp:GridView ID="gvProducts" runat="server" AutoGenerateEditButton="True" AutoGenerateColumns="False"
OnRowEditing="gvProducts_RowEditing" OnRowUpdating="gvProducts_RowUpdating" CellPadding="4"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblPID" runat="server" Text="Product ID"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblPName" runat="server" Text="Product Name"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
and here is the code behind page
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
{
gvProducts.EditIndex = e.NewEditIndex;
}
protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int i = e.RowIndex;
object control = gvProducts.Rows[i].FindControl("txtProdID");
//i want to access the new value from the object "control" but i m getting the previous value only
}
try this
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtProdID = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdID");
TextBox txtProdName = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdName");
//Call update method
Product.Update(txtProdId,txtProdName);
gvProducts.EditIndex = -1;
//Refresh the gridviwe
BindGrid();
}
You need to check the e.NewValues dictionary for updated data.
I've a sample below, the GridView Template is bound to CategoryName.
OnRowUpdating is fired when the 'Edit' button is clicked.
In the RowUpdating event handler, it fetches the CategoryName data to which the textbox is bound to.
Note: Instead of using TextBox in normal mode use Labels.
<asp:GridView ID='GridView1' runat='server' DataKeyNames='CategoryID' OnRowUpdating='HandleOnGridViewRowUpdating'
DataSourceID='ObjectDataSource1' AutoGenerateColumns='false'>
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID='CategoryName' Text='Category' runat='server'></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID='CategoryNameTextbox' Text='<%# Bind("CategoryName") %>' runat='server'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code-behind:
public void HandleOnGridViewRowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (e.NewValues["CategoryName"] != null)
{
String newCategoryName = e.NewValues["CategoryName"].ToString();
// process the data;
}
}