I have bound my gridView with code behind which Contains 4 column but I want first column to be a linkbutton.
So I have added itemtemplate but now my grid contain 5 columns with two same columns, but I want only one column which is of linkbutton column.
This is my itemtemplate code
<Columns>
<asp:TemplateField HeaderText="File No">
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>'
OnCommand="show" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Code behind grid binding :
Bussiness_logic.GridView_Bind(GridView1, "GET_DATA");
public static void GridView_Bind(GridView Grid_Name,String Procdure_name)
{
SqlDataAdapter dap = new SqlDataAdapter(Procdure_name, GetConnection());
DataSet ds = new DataSet();
dap.Fill(ds);
Grid_Name.DataSource = ds;
Grid_Name.DataBind();
}
Your Getting five columns because you Set GridView AutoGenerateColumn to trueAND add an extra template column in grid view.So GridView generate columns for all columns that are present in your data set and add you extra column i.e your template column.
Now to solve this issue Set AutoGenerateColoumn Property of GridView To false. Define Your Column Like below..
<Columns>
<asp:TemplateField HeaderText="File No">
<ItemTemplate >
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("File") %>' Text='<%# Eval("File") %>' OnCommand="show" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameofSecondColumnInYourDateSet" HeaderText="Column2"/>
<asp:BoundField DataField="NameofThirdColumnInYourDateSet" HeaderText="Column3"/>
<asp:BoundField DataField="NameofFourthColumnInYourDateSet" HeaderText="Column4"/>
</Columns>
Related
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.
I want to pass the variable Date to my function HasBtnRights(). Any suggestions on how to do that?
<asp:TemplateField HeaderText="More info" Visible='<%#HasBtnRights(Eval("Date")) %>'>
<ItemTemplate>
<asp:Button runat="server" OnClientClick='openModalPopup(<%# Eval("Agreement")%>);' Text="Open"/>
</ItemTemplate>
</asp:TemplateField>
If you want to hide / show the button based on certain criteria, you can set the Visible property of the Button itself.
<asp:TemplateField HeaderText="More info" >
<ItemTemplate>
<asp:Button runat="server" Visible='<%#HasBtnRights(Eval("Date")) %>' OnClientClick='openModalPopup(<%# Eval("Agreement")%>);' Text="Open" />
</ItemTemplate>
</asp:TemplateField>
An then, in your code behind:
public bool HasBtnRights(DateTime myDate) {
// your date logic here
return true; // or return false;
}
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.
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.
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.