gridview image problem - asp.net

work on C# vs05 asp.net .want to show image on gridview....From Northwind database .Table..Categories .this table contain the picture column......i want to show this column on my gridview.......
my code is bellow.............
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Width="284px" PageSize="3">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Picture">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

You can use an ImageField, like so:
<asp:ImageField DataImageUrlField='<%# "ThumbNailImage.ashx?ImID="+ Eval("Picture") %>'></asp:ImageField>

Related

Need to load data from grid view to text box and droup down using Row command

This is my Grid view. I want to know how to bind data to Text boxes and drop down list using Row command event.
<asp:Panel ID="Panel2" runat="server" CssClass="mid">
<asp:GridView ID="BuyerGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="BuyerId" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="BuyerGrid_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="BuyerName" HeaderText="Buyer Name" />
<asp:BoundField DataField="BuyerCode" HeaderText="Buyer Code" />
<asp:BoundField DataField="CountryName" HeaderText="Country" />
<asp:BoundField DataField="CityName" HeaderText="City" />
<asp:TemplateField HeaderText="" SortExpression="">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="Edit"
CommandArgument='<%#Eval("BuyerId") %>'>Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</asp:Panel>
This is my code behind part.
protected void BuyerGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
//Bind values in the text box of the pop up control
tbxBuyerName.Text = BuyerGrid.Rows[index].Cells[0].Text;
tbxBuerCode.Text = BuyerGrid.Rows[index].Cells[1].Text;
//.Text = gview.Rows[index].Cells[2].Text;
}
}
Change
CommandArgument='<%#Eval("BuyerId") %>'
to
CommandArgument='<%# Container.DataItemIndex %>'
or
CommandArgument='<%# Container.DisplayIndex %>'

Enable RequiredFieldValidation on DetailsView

From some reason the RequiredFieldValidation doesn't work with asp:DetailsView:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="Id" DataSourceID="EntityDataSource1"
DefaultMode="Insert" ForeColor="#333333" GridLines="None"
Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="Id" InsertVisible="false" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="First_Name" HeaderText="Name" SortExpression="First_Name" />
<asp:BoundField DataField="LastName" InsertVisible="false" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:TemplateField HeaderText="Salary" SortExpression="Salary">
<EditItemTemplate>
<asp:TextBox ID="salaryTB" runat="server" Text='<%# Bind("Salary") %>' ValidationGroup="DetailsViewValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvSalary" runat="server" ControlToValidate="salaryTB"
InitialValue="0" Display="Dynamic" ErrorMessage="ss" SetFocusOnError="true" ValidationGroup="DetailsViewValidation"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="JobTitle" InsertVisible="false" HeaderText="JobTitle" SortExpression="JobTitle" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
</asp:DetailsView>
I tried everything I could think of, and, naturally, I searched it online.
When I click on Insert, the value (0) is simply inserted, no questions asked.

How to add Expand/Collapse to a nested Gridview without using JQuery

Is it possible to add expand colapse functionality in a nested grid view without using Jquery or java script.
If yes can someone give me an example?
Below is the asp code for my nested gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Horizontal"
onrowdatabound="GridView1_RowDataBound" DataKeyNames="id1" >
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Students">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
You can use the asp.net ajax toolkit. Which still uses javascript, but you don't have to do any of it yourself. It's all done under the hood.
http://ajaxcontroltoolkit.devexpress.com
Here is the collapse/expand panel extender
http://ajaxcontroltoolkit.devexpress.com/CollapsiblePanel/CollapsiblePanel.aspx

dropdownlist in a footer of gridview

I have asp grid view and i created 4 columns(Id,Name,quantity,price) and a footer. i have bounded all the 4 columns from the database. in the footer template, i added a dropdown list item.
every thing is working fine but, the problem is in the footer templalte dropdownlist is added after all the 4 columns but i want to add the dropdown footer to the 2nd column(Name).
Please apologise for my english n Help me
I used the following code:
<asp:GridView ID="gv_page2" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" ForeColor="Black" GridLines="Vertical" ShowFooter="True">
<AlternatingRowStyle BackColor="#CCCCCC" />
<FooterStyle BackColor="Black" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<Columns>
<asp:BoundField DataField="PRO_ID" HeaderText="PRO_ID" />
<asp:BoundField DataField="NAME" HeaderText="NAME" ItemStyle-Width="100px"/>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate> <asp:TextBox runat="server" Width="100px"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="PRICE" ItemStyle-Width="80px"/>
<asp:BoundField HeaderText="TOTAL_PRICE"/>
<asp:TemplateField>
<FooterTempenter code herelate >
<asp:DropDownList ID="ftrDDL" runat="server" DataTextField="PRO_ID" AppendDataBoundItems="true" DataSourceID="" Width="130px" EnableViewState="true" >
<asp:ListItem Text="SELECT ONE" Value=""></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
you need to modify the template of second column
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Databinder.Eval(Container.DataItem,"Name") %>'
runat="server" Width="100px"></asp:Label></ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrDDL" runat="server" DataTextField="PRO_ID" AppendDataBoundItems="true"
DataSourceID="" Width="130px" EnableViewState="true">
<asp:ListItem Text="SELECT ONE" Value=""></asp:ListItem>
</FooterTemplate>
</asp:TemplateField>
I have not tested the code but this should work .Let me know if it did not work
Is it required you add the drop down list in the footer?
I would suggest adding the drop down to the Name column. The footer will always load last.
Use template filed for Name
<asp:GridView ID="gv_page2" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical" ShowFooter="True">
<AlternatingRowStyle BackColor="#CCCCCC" />
<FooterStyle BackColor="Black" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<Columns>
<asp:BoundField DataField="PRO_ID" HeaderText="PRO_ID" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrDDL" runat="server" DataTextField="PRO_ID" AppendDataBoundItems="true"
DataSourceID="" Width="130px" EnableViewState="true">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="100px"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="PRICE" ItemStyle-Width="80px">
<ItemStyle Width="80px"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="TOTAL_PRICE" />
</Columns>
</asp:GridView>

ASP.NET entity framework code first foreign key on gridview

I am using this method to retrieve company from trade. How can I display company ID on GridView? This is my code to retrieve.
public List<Trade> getTrade()
{
List<Trade> trades=dbContext.trades.Include("tradeCompany")
.OrderBy(t => t.tradeDate).ToList();
return trades;
}
I bind to List at code behind... This is my gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" OnRowDataBound="sharesGridView_RowDataBound" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="tradeDate" DataFormatString="{0:MMMM d, yyyy}"
HeaderText="Date" HtmlEncode="False" SortExpression="tradeDate" />
<asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
<asp:BoundField HeaderText="Company" />
<asp:BoundField DataField="tradePrice" HeaderText="Price"
SortExpression="tradePrice" />
<asp:BoundField DataField="tradeQuantity" HeaderText="Quantity"
SortExpression="tradeQuantity" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
Instead of using a BoundField you can use a template field:
<asp:TemplateField>
<itemtemplate>
<p><%#DataBinder.Eval(Container.DataItem, "Company.Id")%></p>
</itemtemplate>
</asp:TemplateField>

Resources