asp.net:How to place columns from DB inbetween Template Fields of GridView - mysql

Hello i want gridview which should be like
|Sl No|Std No|Student Name|Total Amount|Paid Amount|Unpaid Amount|Action|
Here I am setting Sl No ,Unpaid Amount , and Action Columns by Template Field.And Other i am directly getting from mysql DB and binding to gridview. But the columns from DB are apending to end of those 3 TemplateFields.And i cant use BoundFields for those 4 Fields from DB.
MY CODE IS LIKE THIS
<div class="GridviewDiv">
<asp:GridView runat="server" ID="gvDetails" AllowPaging="false" AutoGenerateColumns="true" Width="100%" OnRowDataBound="GridView1_RowDataBound1">
<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:TemplateField HeaderText="SlNo">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
<asp:Label ID="serialID" runat="server" Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%-- HERE I WANT THOSE COLUMNS FROM DATABASE--%>
<asp:TemplateField HeaderText="Unpaid Amount">
<ItemTemplate>
<asp:Label ID="UnpaidText" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Take Fee">
<ItemTemplate>
<asp:Button ID="TakeFeeCash" runat="server" BackColor="#2C6AA0" BorderColor="#2C6AA0" align="right"
BorderStyle="Ridge" Font-Bold="True" ForeColor="White" OnClick="FeeCashButton_click"
Text="Take Fee" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
What i am getting is like this
IMAGE:
How to set my columns like above format?
Please Help.

Related

Remove duplicate rows from custom SQL select query

I have a table set up that has a unique Match_ID for each data entry. However, when I want to call a custom select query that gathers the team name from the Team table I get duplicate rows listing two Match_IDs that are the same. I want to display rows uniquely in a GridView with columns Home team, Away team, Home team score, Away team score and Game date. I don't want to display a Team's ID in their columns but instead their Team names. However I also want to make sure I can update this GridView, since using AS has prevented me from being able to do so in the past.
Here's what Im using so far with an image showing what the query returns. I am using an SQL datasource.
SELECT MatchStatistics.Match_ID, MatchStatistics.Home_team_ID, MatchStatistics.Away_team_ID, MatchStatistics.Home_team_score, MatchStatistics.Away_team_score, MatchStatistics.Game_date,
Team.Team_name
FROM MatchStatistics INNER JOIN
Team ON MatchStatistics.Home_team_ID = Team.Team_ID OR MatchStatistics.Away_team_ID = Team.Team_ID
WHERE EXISTS
(SELECT DISTINCT Match_ID
FROM MatchStatistics AS MatchStatistics_1)
<asp:GridView ID="EnterMatchGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="Match_ID" DataSourceID="SqlDataSource4" OnRowUpdating="EnterMatchGridView_RowUpdating" OnRowDeleting="EnterMatchGridView_RowDeleting">
<Columns>
<asp:BoundField DataField="Match_ID" HeaderText="Match_ID" SortExpression="Match_ID" InsertVisible="False" ReadOnly="True" />
<asp:TemplateField HeaderText="Home Team" SortExpression="Home_Team_Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Home_Team_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Team_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Away Team" SortExpression="Away_Team_Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Away_Team_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Away_Team_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Home Team Score" SortExpression="Home_team_score">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Home_team_score") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Home_team_score") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Away Team Score" SortExpression="Away_team_score">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Away_team_score") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Away_team_score") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="Game_date">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Game_date", "{0:dd-MM-yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Game_date", "{0:dd-MM-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit/Delete">
<ItemTemplate>
<asp:LinkButton ID="BtnEdit" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit" />
<span onclick="return confirm ('Are you Sure?')">
<asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CausesValidation="true" CommandName="Update" ConflictDetection="OverwriteChanges" Text="Update" ValidationGroup="EnterMatchGridView" />
<asp:Button ID="BtnCancel" runat="server" CausesValidation="false" CommandName="Cancel" ConflictDetection="OverwriteChanges" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In short, how do I make this query return unique a uniqueMATCH_IDfor each row?
Wouldn't that be...
SELECT s.Match_ID
, s.Home_team_ID
, s.Away_team_ID
, s.Home_team_score
, s.Away_team_score
, s.Game_date
, h.Team_name home_team
, a.Team_name away_team
FROM MatchStatistics s
JOIN Team h
ON h.Team_ID = s.Home_team_ID
JOIN Team a
ON a.Team_ID = s.Away_team_ID
???

Can't add TemplateField to Datagrid

I am trying to add a DropDownList into the DataGrid so the location column can be easily changed. I am getting an error with TemplateField, itemtemplate, label and DropDownList. I get the build error:
is not allowed within a
'System.Web.UI.WebControls.DataGridColumnCollection'.
<asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="TrailerOwner" HeaderText="Owner" SortExpression="TrailerOwner"></asp:BoundColumn>
<asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
<asp:TemplateField HeaderText="Trailer Location">
<itemtemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%# Eval("Location") %>' Visible = "false" />
<asp:DropDownList ID="ddlLocation" runat="server">
</asp:DropDownList>
</itemtemplate>
</asp:TemplateField>
<asp:BoundColumn DataField="Year" HeaderText="Year" SortExpression="Year"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
TemplateField, itemtemplate, label and DropDownList all have green lines under them and which means its not a known element.
You should use TemplateColumn, when it comes to DataGrid as it is inherited from System.Web.UI.WebControls.DataGridColumn.
TemplateField is inherited from System.Web.UI.WebControls.DataControlField, which make sense with GridView.

How Listview Item Eval field does not updated in an updatePanel?

I have a list view to show products with prices. I put an updatePanel in ItemTemplate.
When I Click the button in contentTemplate, the Eval fields do not update, By definition the
update panel do a partial post back but it does not reflect database changes.
<ContentTemplate>
<asp:ImageButton ID="productPic" runat="server" Height="270" Width="202" ImageUrl='<%# Eval("ImageUrl") %>' />
<asp:Table ID="ColorTable" runat="server" EnableViewState="true"></asp:Table>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Literal ID="LitProductId" runat="server" Text='<%# Eval("ProductId") %>' EnableViewState="true"></asp:Literal>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>' Font-Size="8"
Font-Bold="False" Font-Names="tahoma" />
<br />
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Price","{0:0,0}") %>' Font-Size="8" Font-Bold="False" Font-Names="tahoma"
Font-Strikeout='<%# Eval("DiscountedPrice")!=DBNull.Value %>' />
<br />
<asp:Label ID="Label4" runat="server" Text='<%# Eval("DiscountedPrice","{0:0,0}") %>' Font-Size="10" Font-Bold="True" Font-Names="tahoma" ForeColor="Red"
Visible='<%# Eval("DiscountedPrice")!=DBNull.Value %>' />
<br />
</ContentTemplate>
</asp:UpdatePanel>
<br />
</td>
I appreciate any help.
It's because you are dynamically adding them in. When you post back, it loses that state and if you aren't adding them back in at that point (i.e. saving them to viewstate and adding them back in on load), it no longer knows they exist.
I found the solution.
If you want to use update panel inside a listview, you should bind the data source in code - page load.
ListView1.DataSource = new ProductFacade().GetCat3ProductListSpec(cat3Id);
ListView1.DataBind();
When you use sqlDataSource attribute, the update panel does not function as you expect.

remove column from grdiview

i want to remove this column from gridview i try to add values in dropdownlist in grdiview but when i add it works fine but this column in black color appears...
how i remove it..
grdiview html
<asp:BoundField DataField="DocType" HeaderText="Document" />
<asp:BoundField DataField="DepType" HeaderText="Department" />
<asp:BoundField HeaderText="ApproveID" DataField="ApproveID"
></asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%#
Eval("ApproveID") %>' Visible = "false" />
<asp:DropDownList ID="DropDownList4" runat="server"
class="vpb_dropdown">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
First make sure AutoGenerateColumns="false" is set on your GridView, and then delete the lines
<asp:BoundField HeaderText="ApproveID" DataField="ApproveID"></asp:BoundField>
from your HTML. When you turn off AutoGenerateColumns, you will need to add each column manually.

String TextBox1 text in a search parameter in SQL statement

I have the following Gridview which I am trying to pass a search parameter, to update the results based on that search -
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" />
<asp:Label ID="Label1" runat="server" Text="Grid not refreshed yet."></asp:Label><br />
<asp:Label ID="Label4" runat="server" Text="(Grid Will Referesh after Every Second)"Font-Bold="true"></asp:Label>
<br /><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDbConn %>"
SelectCommand="SELECT * FROM [table] WHERE BODYTEXT LIKE='"+TextBox1.text+"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
BackColor="Black" DataSourceID="SqlDataSource1" Font-Names="Arial Black"
Font-Size="Small" ForeColor="White" Height="650px" Width="930px">
</asp:GridView>
</ContentTemplate>
However I cant seem to get the SQL statement to accept the TextBox1 text that the user is entering, how can I resolve this?
You must do this in code behind.
SqlDataSource1.SelectCommand = "SELECT * FROM [table] WHERE BODYTEXT LIKE='"+TextBox1.Text;
You can do this when clicking on the search button.