Grid View Styling Asp.Net - asp.net

i want to style the grid view like i have to show 4 orders on the grid view in which 2 order has status true and 2 order has status false now i want to apply style like those order have false status should visible in yellow color and those orders are true should visible in normal color like black here is my grid view
<asp:GridView ID="CateringGridView" runat="server" AutoGenerateColumns="false" DataKeyNames="Id">
<RowStyle CssClass="GridRow" />
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="People Attending">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("PeopleAttending") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Event Type">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("TypeOfEvent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Event Date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Bind("DateOfEvent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my code behind file is
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.FillCateringGrid();
}
}
private void FillCateringGrid()
{
using (CateringOrderDataContext dataContext = new CateringOrderDataContext())
{
var query = dataContext.Caterings
.Where(e => e.Status == true)
.Select(e => e);
this.CateringGridView.DataSource = query;
this.CateringGridView.DataBind();
}
}

<RowStyle CssClass='<%=((Catering)Container.DataItem).Status ? "A" : "B"%>' />
Where A and B are class names based on whether the Status is true or false. then you can define the CSS format for td.A and td.B.

Related

Cells text always getting empty string("") in Gridview rowdatabound

I'm trying to do the disable the delete button for the admin role in the list of users which I had bound in gridview.
So, I went with rowdatabound option where I can disable based on the condition as like checking the text of the role column.
If the role columns text is "Admin" then I had to disable the "Delete" button which I had customized.
Unfortunately I'm not able to get the text of the cells while fetching the data.
I had used the statement as like below :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (e.Row.FindControl("Delbutton") as Button);
if(e.Row.Cells[7].Text == "ADMIN")
{
btn.Enabled = false;
}
}
}
My aspx page :
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
OnPageIndexChanging="GridView1_PageIndexChanging"
PagerSettings-Mode="Numeric"
PagerSettings-PreviousPageText="Previous"
PagerSettings-PageButtonCount="15"
PagerSettings-NextPageText="Next"
PagerStyle-ForeColor="WhiteSmoke"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:button ID="button1" runat="server" Text="Select" OnClick="button1_Click" ></asp:button>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="id" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="idemail" runat="server" Text='<%#Eval("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="idName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Passwor">
<ItemTemplate>
<asp:Label ID="idPassword" runat="server" Text='<%#Eval("Passwor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MobileNumber">
<ItemTemplate>
<asp:Label ID="idMobileNumber" runat="server" Text='<%#Eval("MobileNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="idGender" runat="server" Text='<%#Eval("Gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qulification">
<ItemTemplate>
<asp:Label ID="idQulification" runat="server" Text='<%#Eval("Qulification") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role">
<ItemTemplate>
<asp:Label ID="idRole" runat="server" Text='<%#Eval("Role") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:button ID="Delbutton" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure, Do you want to delete.')" OnClick="Delbutton_Click" ></asp:button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings NextPageText="Next" PageButtonCount="15" PreviousPageText="Previous"></PagerSettings>
</asp:GridView>
Please check the image of my code on debugger
This was my data on page after loaded

hide row on clicking delete link in gridview

I have a gridview, in which two link buttons are there.
Here it is:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="5" OnRowCommand="gv_RowCommand"
AllowPaging="true" DataKeyNames="ID" CssClass="mGrid" BackColor="White" BorderColor="Silver" BorderStyle="Double"
BorderWidth="1px" CellPadding="4" >
<RowStyle BackColor="White" Width="150%" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false" >
<ItemTemplate>
<asp:Label ID="lblID" Text='<%#Bind("ID") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Version">
<ItemTemplate>
<asp:Label ID="lblversion" Text='<%#Bind("version") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image ID">
<ItemTemplate>
<asp:Label ID="lblimageid" runat="server" Text='<%#Bind("image_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Getty ID">
<ItemTemplate>
<asp:Label ID="lblgettyid" runat="server" Text='<%#Bind("getty_id") %>'></asp:Label>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:TextBox ID="txtgettyid" Text='<%#Bind("getty_id") %>' runat="server"></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pool Letter">
<ItemTemplate>
<asp:Label ID="lblpoolletter" runat="server" Text='<%#Bind("pool_letter") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Use">
<ItemTemplate>
<asp:Label ID="lbltouse" runat="server" Text='<%#Bind("to_use") %>'></asp:Label>
</ItemTemplate>
<%-- <EditItemTemplate>
<asp:TextBox ID="txttouse" Text='<%#Bind("to_use") %>' runat="server"></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Clue">
<ItemTemplate>
<asp:Label ID="lblclue" runat="server" Text='<%#Bind("clue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Range">
<ItemTemplate>
<asp:Label ID="lblrange" runat="server" Text='<%#Bind("range") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="last_updated">
<ItemTemplate>
<asp:Label ID="lbllastupdated" runat="server" Text='<%#Bind("last_updated") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<%#Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First">
<ItemTemplate>
<asp:Label ID="lblfirst" runat="server" Text='<%#Bind("first") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle">
<ItemTemplate>
<asp:Label ID="lblmiddle" runat="server" Text='<%#Bind("middle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last">
<ItemTemplate>
<asp:Label ID="lbllast" runat="server" Text='<%#Bind("last") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemStyle Wrap="false" Width="100px" />
<ItemTemplate>
<asp:LinkButton ID="lnkbtnedit" Text="Edit" Visible="true" style="color:#006699;" CommandName="Edit" runat="server"></asp:LinkButton>
<asp:LinkButton ID="lnkbtndel" Text="Delete" Visible="true" style="color:#006699;" CommandName="Delete" runat="server"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#CCFFFF" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
con = new SqlConnection(conString);
con.Open();
int v = version + 1 ;
string updatedetails = "update tblfameface set status ='Deleted',version=" + v + " where ID=" + ID + " ";
SqlCommand cmd = new SqlCommand(updatedetails, con);
int temp = cmd.ExecuteNonQuery();
con.Close();
//int rows = GridView1.CurrentCell.RowIndex;
//GridView1.Rows.RemoveAt(rows);
bindDB();
LinkButton btndel = (LinkButton)GridView1.Rows[row.RowIndex].FindControl("lnkbtndel");
btndel.Visible = false;
}
On clicking Edit link,it is working as desired and
When I click on Delete link, I want that row to be hidden.
which event should be fired and how can it be handled?
how can I achieve this??
This is Aspx:
<Columns>
<asp:TemplateField HeaderText="Hide"> <ItemTemplate> <asp:LinkButton ID="lnkHide" runat="server" Text="Hide" OnClick="LnkHide_Click" /> </ItemTemplate> </asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
This is Code behind:
protected void LnkHide_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
clickedRow.Visible = false;
}
Try this code
private void button1_Click(object sender, EventArgs e)
{
int row = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows.RemoveAt(row);
}
Try this code........

Gridview rowupdating doesn't work

i have a simple task of updating row, on a click of update linkbutton, as
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<% #Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<% #Eval("Mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<% #Eval("LName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #Eval("LName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="action">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">LinkButton</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
at CS page
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox t1 = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
string s = t1.Text.ToString();
Label3.Text = s;
}
I tried working on this several times but i don't get any answer. what possible code i am missing or there is any error in a code.Thanks for any assistance.
Your LinkButton needs to have a CommandName applied to it in the EditTemplate:
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>

'GridView1' fired event PageIndexChanging which wasn't handled

I am using a gridview and I want to use paging. I have already set allow paging to true and page size to 5. I can see the numbers at the base of my gridview, but when i click on a number to move to respective page, it throws an error saying:
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
Code:
<asp:GridView ID="GridView1" runat="server" CellPadding="5"
AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="contact_id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
PageSize="5">
<Columns>
<asp:TemplateField HeaderText="contact_id">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("contact_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="address">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("address") %>'></asp:Label><br />
<asp:Label ID="Label6" runat="server" Text='<%# Eval("city") %>'></asp:Label><br />
<asp:Label ID="Label7" runat="server" Text='<%# Eval("state") %>'></asp:Label><br />
<asp:Label ID="Label8" runat="server" Text='<%# Eval("pincode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="email">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mobile">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="context">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("context") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>PENDING</asp:ListItem>
<asp:ListItem>OK</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemStyle CssClass="button" />
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:GridView>
You will have to handle the PageIndexChanging event for the grid
Something like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//Bind grid
}
It will not work if you enable paging. You also need to write the event handler for PageIndexChangeEvent. Check this link: http://forums.asp.net/post/1177923.aspx
You would need to code "PageIndexChanging" event to make it work. Add an event handler for the PageIndexChanging where you set the GridView.CurrentPage = e.NewPage...
Add one more event in HTML mark up for pagging.
OnPageIndexChanging="GridView1_PageIndexChanging"
Now handle same event from code behind
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//Your code
}

Display text in multiline inside a grid view in asp.net

I am using a grid view in my asp.net application. In one column i need to display description(minimum 5 characters. Maximum 255 characters).i am using a label to hold description in that grid view.
But my problem is that if the description is larger it stretches in the browser and show it in one line. I want to display description in multi line (like a paragraph)
I hope some one help me . the entire grid view code is shown below
<asp:GridView ID="gv_View_Documents" runat="server" AllowSorting="true" DataKeyNames="DocumentName,Description" SkinID="customGridview" AutoGenerateColumns="false" OnSorting="gv_View_Documents_Sorting" OnRowCancelingEdit="gv_View_Documents_RowCancelingEdit" OnRowCommand="gv_View_Documents_RowCommand"
OnRowEditing="gv_View_Documents_RowEditing" OnRowUpdating="gv_View_Documents_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="Document Name" HeaderStyle-Width="200" HeaderStyle-CssClass="GridHeaderStyle" SortExpression="DocumentName" >
<ItemTemplate>
<asp:LinkButton CommandName="ViewDocument" CssClass="GridHeaderStyle" ID="hlnk_View_Document" runat="server" CommandArgument='<%# Bind("DocumentName") %>' Text='<%# Bind("DocumentName") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="200" HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lbl_gv_DocumentDescription" runat="server" Text='<%# Bind("Description") %>' ></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_EditDescription" MaxLength="250" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="50" HeaderStyle-CssClass="GridHeaderStyle" ShowHeader="False" >
<EditItemTemplate>
<asp:LinkButton ID="Bttn_Update_Description" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="Bttn_Cancel_Settings" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton></EditItemTemplate><ItemTemplate>
<asp:LinkButton ID="Bttn_Edit_Description" ForeColor=" #555555" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" ></asp:LinkButton></ItemTemplate><ControlStyle CssClass="edit" />
</asp:TemplateField>
</Columns>
</asp:GridView>
try this... working properly...for wrapping text in Gridview
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;width:100px");
}
}
}
Sometimes ItemStyle Wrap="true" doesn't work. To be certain your text wraps, surround the Label in a and set a width on the surrounding div.
EDIT
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div style="width:100px;">
<asp:Label ID="Label2" runat="server" Text="<%# Eval("Name") %>"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="<%# Eval("Age") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can set the ItemStyle of the TemplateField to true like this:
<ItemStyle Wrap="true" Width="100px" />
Complete gridview code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Wrap="true" Width="100px" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Screenshot:
Try like this by applying the css class
.paraGraphtext
{
white-space: pre-wrap;
}
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>" CssClass="paraGraphtext"></asp:Label>
</ItemTemplate>

Resources