I try to use SortedAscendingCellStyle to assign the style of my gridView this is my ASP Code:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" CellPadding="4" OnSorting="GridView1_Sorting" AutoGenerateColumns="false">
<SortedAscendingCellStyle CssClass="SortedAscendingCellStyle" />
<SortedAscendingHeaderStyle CssClass="SortedAscendingHeaderStyle" />
<SortedDescendingCellStyle CssClass="SortedDescendingCellStyle" />
<SortedDescendingHeaderStyle CssClass="SortedDescendingHeaderStyle " />
<Columns>
<asp:BoundField DataField="Nom" HeaderText="Nom" SortExpression="NOM">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Prenom" HeaderText="Prenom">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Pseudo" HeaderText="Pseudo"></asp:BoundField>
<asp:BoundField DataField="Mail" HeaderText="Mail">
<ItemStyle Width="140px" />
</asp:BoundField>
</Columns>
</asp:GridView>
there is no problem when i use DataSourceID, but when I use server side databining the SortedAscendingCellStyle Did Not work, this my code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
EmployeDAL oem = new EmployeDAL();
this.GridView1.DataSource = oem.GetAll();
GridView1.DataBind();
}
}
From my research it appears that you cannot use these nice attributes you when manually bind your GridView in the code-behind. You can find another discussion on this topic here:
http://forums.asp.net/t/1725183.aspx/1
...and also here...
http://www.aarongoldenthal.com/post/2009/04/19/Manually-Databinding-a-GridView.aspx
Good luck! I'm trying to figure out a solution to the same problem..
Related
I have a gridview in the front end showing 4-5 columns. Need to hide one of the column when the values are NULL.
<asp:GridView ID="gvProducts" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" AutoGenerateColumns="False" AllowPaging="True" PageSize="100" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CategoryId" SortExpression="CategoryId" HeaderText="CategoryId" Visible="false" />
<asp:BoundField DataField="MerchantName" HeaderText="MerchantName" SortExpression="MerchantName" />
<asp:BoundField DataField="StoreName" HeaderText="StoreName" SortExpression="StoreName" />
<asp:BoundField DataField="StoreAddress" HeaderText="StoreAddress" SortExpression="StoreAddress" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True"
ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
If I want to hide StoreName column from code-behind. How to achieve that ?
You can Use RowDataBound Event of GridView
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string val = e.Row.Cells[0].ToString(); //check first cell value
if(string.IsNullorEmpty(val) )
{
gvSearchEngine.Columns[0].Visible = false; //Hides First Column
}
}
}
Yes, you can dynamically create the boundfield values
from code behind based on your requirment
follow this link
add boundField to gridview in codebehind file C#
try e.Row.Cells[0].Controls[0].Visible = false;
When clicking the select button in gvSquid2, x gets a valid and correct value. When clicking the select button in gvSquid, I get an ArgumentOutOfRange Exception because gv.SelectedIndex=0. Additionally, gv.SelectedDataKey is null. How do I get the DataKey information? Putting it in a column is not acceptable, as it must be hidden from the user. Hiding the column is useless, because during DataBind(), the value is discarded. I ripped this example from Microsoft's website. What am I missing?
<asp:gridview ID="gvSquid2" DataSourceID="dsComments" AutoGenerateColumns="false" AutoGenerateSelectButton="true" DataKeyNames="ID" OnSelectedIndexChanged="gvSquid_SelectedIndexChanged" runat="server" EnableViewState="false">
<Columns>
<asp:BoundField DataField="Date" ReadOnly="true" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Comment" ReadOnly="true" HeaderText="Comment" SortExpression="Comment" />
<asp:BoundField DataField="Username" ReadOnly="true" HeaderText="User" SortExpression="Username" />
</Columns>
</asp:gridview>
<asp:gridview ID="gvSquid" DataSourceID="dsComments" AutoGenerateColumns="false" DataKeyNames="ID" OnSelectedIndexChanged="gvSquid_SelectedIndexChanged" runat="server" EnableViewState="false">
<Columns>
<asp:CommandField ButtonType="Image" HeaderText="Select" ShowSelectButton="true" SelectImageUrl="~/includes/RedX.jpg" />
<asp:BoundField DataField="Date" ReadOnly="true" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Comment" ReadOnly="true" HeaderText="Comment" SortExpression="Comment" />
<asp:BoundField DataField="Username" ReadOnly="true" HeaderText="User" SortExpression="Username" />
</Columns>
</asp:gridview>
protected void gvSquid_SelectedIndexChanged(object sender, EventArgs e) {
string x;
x = gvSquid.DataKeys[gvSquid.SelectedIndex].Value.ToString();
}
Try using the SelectedRow of the grid view to get to the RowIndex, like this:
protected void gvSquid_SelectedIndexChanged(object sender, EventArgs e)
{
string x;
GridViewRow theGridViewRow = gvSquid.SelectedRow;
x = gvSquid.DataKeys[theGridViewRow.RowIndex].Value.ToString();
}
I figured it out. EnableViewState="true" has to be set. Once I changed that, suddenly the DataKeys appeared.
This question helped.
<asp:GridView ID="GridView1" class="datagrid" runat="server" AutoGenerateColumns="False" width="90%" Height="400px"
OnRowCommand="GridView1_RowCommand" BackColor="White"
BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" CellPadding="4">
<Columns>
<asp:TemplateField HeaderText="File" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False"
CommandArgument='<%# Eval("File") %>'
CommandName="Download" Text='<%# Eval("File") %>'
onclick="LinkButton1_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Size" HeaderText="Size in Bytes" />
<asp:BoundField DataField="Type" HeaderText="File Type" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099"
HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
i have this download function and upload
i want to include a download verification code where there will be a textbox and button if the characters from textbox is incorrect download will be disable.
please note that in the file section i have the linkbutton that enables the user to download the file
Try the code below, should be written in code behind file:
protected void GrivView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButtton lbtn = new LinkButtton();
lbtn = (LinkButton)e.Row.FindControl("LinkButton1");
lbtn.Enable = false;
}
I think this line of code is enough
((LinkButton)e.Row.Cells[1].Controls[0]).Visible = false;
In this way we can disable or enable linkbutton or any other control dynamically
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
/*if (<condtition)
* {
* */
GridView1.Columns[3].Visible = false;
// * }
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//make it again visible
GridView1.Columns[3].Visible = true;
// code to edit the rows
}
I hav a gridview inside a usercontrol
<asp:GridView ID="grdMissingFilterData" runat="server" AllowPaging="True" Width="100%"
AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
PageSize="30" OnPageIndexChanging="grdMissingFilterData_PageIndexChanging">
<Columns>
<asp:BoundField DataField="Varenummer" HeaderText="Varenummer" ItemStyle-Width="25%" >
<ItemStyle Width="25%" />
</asp:BoundField>
<asp:BoundField DataField="Varenavn" HeaderText="Varenavn" ItemStyle-Width="15%" >
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="Producentvarenummer" HeaderText="Producent varenummer" ItemStyle-Width="15%" >
<ItemStyle Width="15%" />
</asp:BoundField>
</Columns>
<AlternatingRowStyle CssClass="altrow" />
<PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="50" />
<EmptyDataTemplate>
There is no data available to display!
</EmptyDataTemplate>
<PagerStyle CssClass="pager" />
</asp:GridView>
and code in postback
if (!Page.IsPostBack)
{
BindData();
}
then i had a PageIndexChanging event which never fires when i click on paging.
protected void grdMissingFilterData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdMissingFilterData.PageIndex = e.NewPageIndex;
BindData();
}
can any one give me any possible reasons?
This may be probably an issue with your user control not about gridview.
Focus on that part
This is my aspx:
<asp:UpdatePanel ID="resultPanel" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="AddDocument" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" EnableSortingAndPagingCallbacks="True"
AllowPaging="True" DataSourceID="FilesObjectDataSource" PageSize="5" OnRowCommand="gridView_RowCommand"
DataKeyNames="FileGuid" HorizontalAlign="Left" Width="100%" BorderStyle="Solid"
BorderColor="Black">
<Columns>
<asp:BoundField DataField="RID" HeaderText="ID" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="Category" HeaderText="SubCategory" ReadOnly="True">
</asp:BoundField>
<asp:BoundField DataField="FileTypeName" HeaderText="Type" ReadOnly="True">
</asp:BoundField>
<asp:BoundField DataField="FileGUID" Visible="false" />
<asp:ButtonField Text="X" ButtonType="Button" ItemStyle-Width="20px" CommandName="DelFile">
<ItemStyle Width="20px" />
</asp:ButtonField>
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
</ContentTemplate>
This is my code behind for the ButtonField marked 'X'
protected void gridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DelFile")
{
//Selected Row
int rowIndex = Convert.ToInt32(e.CommandArgument);
fileGuid = new Guid(gridView.DataKeys[rowIndex].Values["FileGuid"].ToString());
}
}
Sometimes itt results in object ref not set and at times it works. Not sure why this happens in production and not in dev and testing.
I'm not sure if it makes any difference, but but the case in ("FileGuid")
DataKeyNames="FileGuid"
and
fileGuid = new Guid(gridView.DataKeys[rowIndex].Values["FileGuid"].ToString());
doesn't match ("FileGUID")
<asp:BoundField DataField="FileGUID" Visible="false" />
You can use like this for accessing value at row command event
protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DelFile")
{
int index = Convert.ToInt32(e.CommandArgument);
int documentID = Convert.ToInt32(gridView.DataKeys[index].Value);
// Write your further code
}
}