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>
Related
I have gridview,which has approve and reject button.When click on reject button i need to show textbox for comments else no.after enetering comments i have save it in data base.How to do this can any one help me
here is my code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Industrial Zone">
<ItemTemplate>
<asp:Label runat="server" ID="lblindzone" Text='<%# Eval("indzone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="District">
<ItemTemplate>
<asp:Label runat="server" ID="lbldstr" Text='<%# Eval("dstr")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Industrial Area">
<ItemTemplate>
<asp:Label runat="server" ID="lblnmindar" Text='<%# Eval("nmindar")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plot Number">
<ItemTemplate>
<asp:Label runat="server" ID="lblplno" Text='<%# Eval("plno")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:LinkButton ID="lnkApproved" runat="server" Text="Approve" OnClick="lnkApproved_Click"><img src="images/approve.png" style="width:20px;height:20px; margin:5px" title="Approve"/></asp:LinkButton>
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
<asp:LinkButton ID="lnkviewdetails" runat="server" Text="View Details" OnClick="lnkviewdetails_Click"><img src="images/viewdetails.png" style="width:20px;height:20px; margin:5px" title="Details" /></asp:LinkButton>
<asp:TextBox ID="txtcomment" runat="server" style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="lblstatus" Text='<%# Eval("status")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am assuming you are doing it with only C# and no Javascript. to view or hide a specific textbox you should trigger a RowCommand Event of GridView:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "viewhide")
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tbComments = ((TextBox)gvr.FindControl("txtcomment"));
tbComments.Visible = true;
}
}
And this would be your ASPX Codebehind
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" CommandName="viewhide" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
EDIT: Now You've changed the tag from C# to VB.NET which is not good. because i consumed all my time writing a c# code!
I have this Select statement:
SELECT recordID As [Zap.st.] FROM SomeTable
When I try to bind the result to a GridView with template fields using an asp:Label I get this error:
System.Web.HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Zap'
This is a sample GridView:
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("Zap.st.") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried:
Text='<%# Eval("[Zap.st.]") %>'
Text='<%# Eval("'Zap.st.'") %>'
but none of these seem to work.
Edit: To further clarify my problem:
My Select statement is a View on Sql Server, the results in my asp.net come in as [Zap.st.], there is no reference that this is [recordID]
Try this code :
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("recordID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I found a similar question on SO, this is the solution:
<asp:Label ID="lblZapStD" runat="server" Text='<%# DataBinder.GetPropertyValue(Container.DataItem, "Zap.st.") %>' />
And this is the original question with an accepted answer.
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:
I am getting this exception at runtime which says it cannot find a column 'M' in datasource, but i am not using 'M' anywhere. I am trying to bind data to dropdownlist inside gridview.
I need to do this in .aspx page rather than code behind.
Here's the code i am using:
<asp:GridView ID="grdDrpDownlistSample" runat="server" AutoGenerateColumns="false" DataSourceID="sqlDS1">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logged In Status">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" Checked='<%# Eval("LoggedIn") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex">
<ItemTemplate>
<asp:DropDownList ID="drpSex" DataSourceID="sqlDS1" runat="server" DataTextField='<%# Eval("Sex") %>' DataValueField='<%# Eval("id") %>' ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlDS1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select * from Users"> </asp:SqlDataSource>
It's working now.
I have got this working by removing Eval from dropdownlist, as it was expecting just the column name not the Eval expression.
I have a very standard Gridview, with Edit and Delete buttons auto-generated.
It is bound to a tableadapter which is linked to my RelationshipTypes table.
dbo.RelationshipTypes:
ID, Name, OriginConfigTypeID, DestinationConfigTypeID
I wish to use a label that will pull the name from the ConfigTypes table, using the OriginConfigTypeID and DestinationTypeID as the link.
dbo.ConfigTypes:
ID, Name
My problem is, I can't automatically generate Edit and Delete buttons using an Inner Join in my dataset. Or can I?
Here is my code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CssClass="TableList"
DataKeyNames="ID" DataSourceID="dsRelationShipTypes1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" Visible=False/>
<asp:TemplateField HeaderText="Origin" SortExpression="OriginCIType_ID">
<EditItemTemplate>
<asp:DropDownList Enabled=true ID="DropDownList2" runat="server" DataSourceID="dsCIType1"
DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("OriginCIType_ID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("OriginCIType_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Destination" SortExpression="DestinationCIType_ID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="dsCIType1" DataTextField="Name"
DataValueField="ID" SelectedValue='<%# Bind("DestinationCIType_ID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DestinationCIType_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So I did try to create my own edit and delete buttons, but kept receiving the error
"cannot find update method"
or something similar. Do I have to manually code the delete and update methods in my code-behind?
You have to tell either the ObjectDataSource what object to use or the SQLDataSource what stored proc to use. Use the "UpdateMethod" attribute.
You can use the code behind technique to mention about what are the method that could handle the update and delete function. This is the standard way to do that. You can either use the Sqldatasorce to describe the source.You can mention which all the tables used for inner join and can also use the sql query.