I would like to sort my GridView by clicking on the column's header. So far I declared a SPDataSource
<SharePoint:SPDataSource
runat="server"
ID="SPdsInventarGrid"
DataSourceMode="List"
UseInternalName="true"
Scope="Recursive"
SelectCommand="<View><Query><Where><Or><Neq><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x00210' /><Value Type='Text'>Open</Value></Neq><IsNull><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x0028' /></IsNull></Or></Where></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='ID'/><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x0028'/><FieldRef Name='EBS_x002e_CN_x002e_Inventar_x00210'/><FieldRef </ViewFields></View>">
<SelectParameters>
<asp:Parameter Name="ListName" DefaultValue="Inventar" />
</SelectParameters>
</SharePoint:SPDataSource>
Then I created my grid, based on the datasource
<asp:GridView Visible="true" Width="100%"
ID="gvInventar"
AutoGenerateColumns="false" runat="server" CellPadding="2" AllowPaging="false" AllowSorting="true" OnSorted="gvInventar_Sorted" OnSorting="gvInventar_Sorting" GridLines="None" DataSourceID="SPdsInventarGrid" OnRowCommand="gvInventar_RowCommand">
<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="true" EditImageUrl="/_layouts/images/edit.gif" UpdateImageUrl="/_layouts/images/save.gif" CancelImageUrl="/_layouts/images/delete.gif" />
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" >
<ItemTemplate>
<asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product" SortExpression="Product">
<ItemTemplate>
<asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server" ID="lblProduct" "></asp:Label>
</ItemTemplate>
</Columns>
</asp:GridView>
Now, I have this function where I wanted to get the current Query as a string and add <OrderBy>(my field) before <Query> but i get an error saying that the DataSource has been already declared (which is true because i want to be declared in asp)
protected void gvInventar_Sorting(object sender, GridViewSortEventArgs e)
{
SPQuery q = new SPQuery();
q.Query = SPdsInventarGrid.SelectCommand;
switch (e.SortExpression)
{
case "Product":
if (e.SortDirection == SortDirection.Ascending)
{
gvInventar.DataSource = q.Query;
gvInventar.DataBind();
}
else
{
}
break;
}
}
Even though it's not working this way, I don't think modifying the Query is the solution.
Could anyone help me with this matter?
You don't need to write any custom code to sort the gridview by clicking the column headers, all you have to do is set the SortExpression property of the TemplateField to the required column name, e.g:
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<ItemTemplate>
<asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product" SortExpression="EBS_x002e_CN_x002e_Inventar_x0028">
<ItemTemplate>
<asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server"
ID="lblProduct" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
Now you can sort on all 3 columns:
Related
My page has a GridView for managing Factory Plants. I'm able to successfully edit the data in the Grid by clicking the edit link and then saving my changes upon clicking the update link. I want the ID column of the Grid to be hidden from the user, so I added the Visible Property to the Plant_ID column of the Grid and set it to false, the column is hidden, but I get an error: "Input string was not in a correct format" upon hitting the update link.
This is my Grid and SQL data Source:
<asp:GridView ID="GridPlants" runat="server" DataKeyNames="Plant_ID" AllowPaging="True" PageSize="7"
AllowSorting="True" AutoGenerateColumns="False" AutoGenerateEditButton="True"
BorderWidth="2px" CellPadding="2" CssClass="datatable" GridLines="None"
ShowFooter="True" SortedAscendingCellStyle-CssClass="sortasc" OnSorting="gvPlant_Sorting"
SortedAscendingHeaderStyle-CssClass="sortasc"
SortedDescendingCellStyle-CssClass="sortdesc"
SortedDescendingHeaderStyle-CssClass="sortdesc"
OnRowCancelingEdit="GridPlants_RowCancelingEdit"
OnRowEditing="GridPlants_RowEditing" OnRowUpdating="GridPlants_RowUpdating" OnPageIndexChanging="gvPlants_PageIndexChanging"
>
<EditRowStyle />
<PagerStyle CssClass="pager-row" />
<RowStyle CssClass="row" />
<Columns>
<asp:BoundField DataField="Plant_ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="Plant_ID" Visible="false" />
<asp:TemplateField HeaderText="Plant Name" SortExpression="Plant_Name">
<EditItemTemplate>
<asp:TextBox ID="Txtplant" runat="server" Text='<%# Bind("Plant_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Plant_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plant Code" SortExpression="Plant_code">
<EditItemTemplate>
<asp:TextBox ID="Txtcode" runat="server" Text='<%# Bind("Plant_code") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Plant_code") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="Address">
<EditItemTemplate>
<asp:TextBox ID="Txtaddress" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<EditItemTemplate>
<asp:TextBox ID="Txtcity" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:TextBox ID="Txtstate" runat="server" Text='<%# Bind("State") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("State") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Zip" SortExpression="Zip">
<EditItemTemplate>
<asp:TextBox ID="Txtzip" runat="server" Text='<%# Bind("Zip") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Zip") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plant Email" SortExpression="Plant_Email">
<EditItemTemplate>
<asp:TextBox ID="Txtemail" runat="server" Text='<%# Bind("Plant_Email") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Plant_Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active" SortExpression="Active">
<EditItemTemplate>
<asp:CheckBox ID="Chkactive" runat="server" Checked='<%# Bind("Active") %>'
Enabled="false" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("Active") %>'
Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UpdateBy" HeaderText="Update By"
ReadOnly="True" SortExpression="UpdateBy" />
<asp:BoundField DataField="UpdateDate" HeaderText="Update Date" ReadOnly="True"
SortExpression="UpdateDate" />
</Columns>
<%-- <PagerSettings FirstPageText="«" LastPageText="»" Mode="NumericFirstLast"
PageButtonCount="7" />--%>
<SortedAscendingCellStyle CssClass="sortasc" />
<SortedAscendingHeaderStyle CssClass="sortasc" />
<SortedDescendingCellStyle CssClass="sortdesc" />
<SortedDescendingHeaderStyle CssClass="sortdesc" />
</asp:GridView>
<asp:SqlDataSource ID="PlantDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ShipmentNotification %>"
SelectCommand="select Plant_ID, Plant_Name, Plant_code, Address, City, State, Zip, Plant_Email, Active, UpdateDate, UpdateBy from Plant order by plant_code">
</asp:SqlDataSource>
This is my Update function:
Protected Sub GridPlants_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridPlants.RowUpdating
Dim Editrow As GridViewRow = GridPlants.Rows(e.RowIndex)
Dim PlantId As String = GridPlants.DataKeys(e.RowIndex)("Plant_ID")
Dim textplant As TextBox = Editrow.FindControl("txtplant")
Dim textcode As TextBox = Editrow.FindControl("txtcode")
Dim checkbox As CheckBox = Editrow.FindControl("chkactive")
Dim address As TextBox = Editrow.FindControl("txtaddress")
Dim city As TextBox = Editrow.FindControl("txtcity")
Dim state As TextBox = Editrow.FindControl("txtstate")
Dim zip As TextBox = Editrow.FindControl("txtzip")
Dim email As TextBox = Editrow.FindControl("txtemail")
'I get an error on the following line:
DL.AddUpdatePlants("Admin", "Edit", Editrow.Cells(1).Text, textplant.Text, textcode.Text, address.Text, city.Text, state.Text, zip.Text, email.Text, checkbox.Checked)
'CType(Editrow.Cells(2).Controls(0), TextBox)).
GridPlants.EditIndex = -1
BindData()
End Sub
...Snippet of the stored procedure:
Dim myCommand As New SqlCommand("SavePlantDetails", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("#editType", addupdate)
myCommand.Parameters.AddWithValue("#Plant_ID", plant_id)
myCommand.Parameters.AddWithValue("#Plant_Name", name)
myCommand.Parameters.AddWithValue("#Plant_code", code)
myCommand.Parameters.AddWithValue("#address", address)
myCommand.Parameters.AddWithValue("#city", city)
myCommand.Parameters.AddWithValue("#state", state)
myCommand.Parameters.AddWithValue("#zip", zip)
myCommand.Parameters.AddWithValue("#email", email)
myCommand.Parameters.AddWithValue("#username", username)
myCommand.Parameters.AddWithValue("#Active", active)
myCommand.ExecuteNonQuery()
myConnection.Close()
...I'm wondering if by hiding the Plant_ID field, is that throwing off how I have my columns arranged? Could I get some help with this issue please? I only get the error after added the Visible property to the Plant_ID column and setting it to True. Thanks in advance.
Your value for Editrow.Cells(1).Text may not be coming up with visible false.
You can use the DataKeyNames property of the grid view to retrive this value.
set it as follows:
<asp:GridView ID="GridPlants" runat="server" DataKeyNames="Plant_ID" /> // rest of gridview markup
retrieve it as follows:
Dim PlantId as String = GridPlants.DataKeys(e.RowIndex)("Plant_ID")
and use this in your update function.
The Concept:
Editrow.Cells(1).Text
or in General:
rows.Cells(index).Text can ONLY be used to read data from a <asp:BoundField> field.
If you have TemplateFields as in <ItemTemplate> and <EditItemTemplate>, You must use the FindControl() method.
Here you have set AutoGenerateEditButton="true", thus your first Column ( Cell Zero obviously ) will contain the edit buttons actually. Starting with 2nd Column ( Cell 1) will be your Fields as you defined in your GridView Markup.
CASE I::
When you have set Visible="true" for your first <asp:BoundField> field, then Cells(1).Text actually refers to the Text inside Your Bound Field which is correct.
CASE II::
However, when you set Visible="false" for your first <asp:BoundField> field, then Cells(1).Text NO more refers to the first BoundField, but actually it's the next TemplateField: Plant_Name. The Data/Text in case of <asp:TemplateField> Column isn't inside the Cells but inside the Controls placed in these TemplateFields. This is the reason you get an error.
I have two textbox inside gridview. If 1st text contains any data then 2nd textbox should not be empty. If 1st textbox is null then no validation for 2nd textbox. Only the alert will come after filling the 1st textbox 2nd textbox shouldn't empty. How to do that?
Any idea? Please help me out.
<asp:GridView runat="server" Width="980px" ID="grdResUpdate" AutoGenerateColumns="false"
CssClass="TopMargin10 borderClass gridwrap" OnRowDataBound="grdResUpdate_RowDataBound" ShowHeader="true">
<Columns>
<asp:TemplateField HeaderText="SO #" ItemStyle-Width="70px">
<ItemTemplate>
<asp:Label runat="server" Width="70px" ID="lblSOName" CssClass="gridwrap" Text='<%# Eval("SOName")%>' />
<asp:HiddenField runat="server" ID="hdnFldSOId" Value='<%#Eval("SOId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Type" ItemStyle-Width="300px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:HiddenField runat="server" ID="testTypeIdHdnFld" Value='<%#Eval("TestTypeId") %>' />
<asp:Label runat="server" Width="150px" ID="lblTestTypeName" CssClass="gridwrap" Text='<%# Eval("TestTypeName")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Case Count" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtTestCaseCount" runat="server" HeaderText="Test Case Count" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Environment" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtEnvironment" runat="server" HeaderText="Environment" Width="80px" onkeydown="return isDigit(event)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Passed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtPassed" runat="server" HeaderText="Passed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Failed" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtFailed" runat="server" HeaderText="Failed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Of Defects" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtDefects" runat="server" HeaderText="No Of Defects" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hours" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtHours" runat="server" HeaderText="Hours" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Case Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtTCComp" runat="server" HeaderText="Test Case Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Percentage Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtPercComp" runat="server" HeaderText="Percentage Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerTop" />
<AlternatingRowStyle CssClass="rowbgblue" />
<RowStyle CssClass="rowbginwhite" />
</asp:GridView>
I want when the user fills the txtTestCaseCount then he must have to fill txtHours
can check with onfoucsOut event
$("textbox2id").focusOut(function(){
if(!($("textbox2id").val() == ""))
{
alert("please enter a value as required");
$(this).focus();
}
})
First access gridview text boxes and then place checking code inside change event of txtTestCaseCount textbox
var textBoxOne = '#<%=grdResUpdate.ClientID%> input[id*="txtTestCaseCount"]';
var textBoxTwo = '#<%=grdResUpdate.ClientID%> input[id*="txtHours"]';
$(textBoxOne).on('change', function () {
if($(textBoxTwo).val()==""){
alert("Please fill hours textbox");
}
});
this checks if txtTestCaseCount value is filled then if textbox txtHours is not filled then alert message is shown.
Every textbox in gridview has an id generated at runtime. Use that id as a selector, like
$("#textboxid").val()
.This will give u the content of the textbox. Use it to validate the other textbox.
If suppose the id generated of txtTestCaseCount is like grdResUpdate_txtTestCaseCount then
if($("#grdResUpdate_txtTestCaseCount").val().length < 0){//Code}
I am trying to build a simple GridView.
I have a Products table and it contains ProductID and ModelName.
I am using LINQ to SQL
My GridView code is
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my datasource code is
<asp:ObjectDataSource ID="objData" runat="server" SelectMethod="GetAll" UpdateMethod="UpdateProduct"
DataObjectTypeName="TestLINQProto.tblProduct" TypeName="TestLINQProto.ProductsList"></asp:ObjectDataSource>
I have my update method written like this
[DataObjectMethod(DataObjectMethodType.Update,true)]
public void UpdateProduct(tblProduct product)
{
EshopDataAccess.UpdateProduct(product);//This will call a LINQ function to update the product
}
The issue is that the tblProduct that the UpdateProduct contain original values. It is not getting the updated values.
I think the issue is that you are using Eval method which only supports reading/fetching of data. Use Bind Method in your grid aspx code. Like this
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Bind("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have user control with a gridview, but for some reason I can't use the asp controls from within the backend code.
In the other user controls I can use them normally but at this particular user control it gives me error when I am trying to use a control which is used in the gridview.
Here is the code for my gridview, please let me know if you notice something unusual:
<asp:GridView ID="gvGDG" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1" Width="100%" CssClass="mGrid" GridLines="None" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
AllowPaging="True" >
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ItemStyle-HorizontalAlign="Center" CausesValidation="False">
</asp:CommandField>
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True"
ItemStyle-HorizontalAlign="Center" >
</asp:BoundField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:UpdatePanel ID="countrypanel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" DataTextField="name" DataValueField="ID" onselectedindexchanged="ddlcountry_SelectedIndexChanged" AppendDataBoundItems="true" AutoPostBack="true" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCountry" />
</Triggers>
</asp:UpdatePanel>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="State Province" SortExpression="State_Province">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("State_Province") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="SqlDataSource3" SelectedValue='<%# Bind("State_Province") %>'
DataTextField="StateName" DataValueField="StateName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [StateName] FROM [States]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="GDG Type" SortExpression="State_Province">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("GDG_Type") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlGdg" runat="server" DataSourceID="SqlDataSource4" SelectedValue='<%# Bind("GDG_Type") %>'
DataTextField="GDG" DataValueField="GDG">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [GDG] FROM [GDG]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Energy Type UOM" SortExpression="Energy_Type_UOM">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Energy_Type_UOM") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUomEnergy" runat="server" DataSourceID="SqlDataSource5" SelectedValue='<%# Bind("Energy_Type_UOM") %>'
DataTextField="UOM" DataValueField="UOM">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT UOM FROM UOM WHERE (Type = 'E')"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="GDG UOM" SortExpression="GDG_UOM">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("GDG_UOM") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlUom" runat="server" DataSourceID="SqlDataSource6" SelectedValue='<%# Bind("GDG_UOM") %>'
DataTextField="UOM" DataValueField="UOM">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT UOM FROM UOM WHERE (Type = 'O')"></asp:SqlDataSource>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="GDG_Coeficient_Value"
HeaderText="GDG Coeficient Value" SortExpression="GDG Coeficient Value"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
Thanks in advance, Laziale
If I m right then. You have a User Control. Say wucMyControl.ascx, where you have a gridview control gvGDG. And u are unable to access it from wucMyControl.ascx.cs. If its the problem then use the solution below:
In your user controls designer file check the access level of the Gridview control. Might be it will get changed to private, accidently , instead of protected. If its private , make it protected or public and check whether you are able to access it now or not.
Update
FYI, you can't access directly the child controls. For this you need to access in the controls DataBound event.
Here in your case :
protected void GridView_RowDataBound(sender, e)
{
if(e.Row.RowType == DataControlRowtype.DataRow)
{
var ddl = (DropdownList)e.Row.FindControl("ddlCountry");
// do whatever you want with ddl. Similarly you can find all html and server control inside a gridview.
}
}
This will help you...
Adding event to gridview child controls
In your GridView1_RowDataBound method, add the handler when you create the drop down by calling ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
Then, declare the void ddl_SelectedIndexChanged(object sender, EventArgs e) method to handle your logic. The sender argument will be a reference to the drop down's that was selected. Also set AutoPostback property of the drodownlist to true.
I think maybe you have some wrong with code on top line of page ASP.net designer:
Please check on CodeFile="??????" and Inherits="????????"
I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).