I have the following GridView in my project:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="329px">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="imie" HeaderText="imie" ReadOnly="True" SortExpression="imie" />
<asp:BoundField DataField="nazwisko" HeaderText="nazwisko" ReadOnly="True" SortExpression="nazwisko" />
<asp:BoundField DataField="idz" HeaderText="idz" ReadOnly="True" SortExpression="idz" />
<asp:BoundField DataField="idp" HeaderText="idp" ReadOnly="True" SortExpression="idp" />
<asp:BoundField DataField="ids" HeaderText="ids" ReadOnly="True" SortExpression="ids" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="ProjektZaliczenieKoncowy.DBClassesDataContext" EntityTypeName="" OrderBy="id" Select="new (id, imie, nazwisko, idz, idp, ids)" TableName="pracownicies">
</asp:LinqDataSource>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
In field "idz" I want to show the date from the column "Zawod" in the table "Zawody" where idz = id. It should be a one to one relationship.
Here's my database diagram:
Diagram
It's probably easy but I can't find any solutions.
You should make a stored procedure in your database which gets you all the columns you need with the one "Zawod" in the table "Zawody" where idz = id. After that you call this stored procedure with c#.
In your code you should have object which represents the entities from the database. You get the information from the database parse it to an object and after that you bind your list of these object to your gridview and there you can get the column you want from the other table.
If you want to do it without a stored procedure you can just write manually the text of the query and pass it as SQLCommand the continuation is the same anyway so the stored procedure is preferable.
Related
I would like to apologize if this has been asked in a different way before. I couldn't find any links specific to this question.
I'm currently learning how to implement GridViews on an ASP.Net web forms application, changing to grid views from a repeater. The repeaters had been binding to some columns retrieved by a stored procedure on our SQL Server and then displaying to the page. Due to the large amount of data, we decided that using the paging feature associated with grid views would improve our performance. However, when I have the grid view bind to the data source, all of the columns from the stored procedure are displayed. I believe I have the syntax correct:
<asp:GridView ID="CECreditGridView" runat="server" DataSourceID="testSqlSource" AllowPaging="true" PageSize="100">
<Columns>
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkView" />
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Update.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkEdit" />
<asp:HyperLinkField DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" ItemStyle-CssClass="actionLinkDelete" />
<asp:BoundField HeaderText="ID" DataField="CECredit_ID"/>
<asp:HyperLinkField HeaderText="Session Title" DataNavigateUrlFormatString="CECredit_Detail.aspx?CECredit_ID={0}" DataNavigateUrlFields="CECredit_ID" DataTextField="CECredit_Name" />
<asp:HyperLinkField HeaderText="Person" DataNavigateUrlFormatString="Person_Update.aspx?Person_ID={0}" DataNavigateUrlFields="Person_ID" DataTextField="FullName" />
<asp:HyperLinkField HeaderText="Type" DataNavigateUrlFormatString=“CECreditType_Update.aspx?CECreditType_ID={0}" DataNavigateUrlFields="CECreditType_ID" DataTextField="CECreditType_Name" />
<asp:HyperLinkField HeaderText="Status" DataNavigateUrlFormatString="CECreditStatus_Update.aspx?CECreditStatus_ID={0}" DataNavigateUrlFields="CECreditStatus_ID" DataTextField="CECreditStatus_Name" />
<asp:BoundField HeaderText="Expiration Date" DataFormatString = "{0:MM/dd/yyyy}" DataField="CECredit_ExpirationDate" />
<asp:BoundField HeaderText="Last Updated" DataFormatString = "{0:MM/dd/yyyy}" DataField="CECredit_RecordUpdateDate" />
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" />
</asp:GridView>
So my question is, is there a way to only have the called columns display? Or will I need to change the stored procedure to only return the columns I want displayed? We were trying to have the stored procedure stay the same because the code behind used a few of the other columns for validation, etc.
How to solve this error as I Just use the GridView to do my update, but prompt out this error.
In the Add.apsx.cs the date I set toString("yyyy-MM-dd") to store in database.
But Still come out this error when I want to update.
I have to put what inside my gridview to solve this problem?
smdSelect.Parameters.AddWithValue("#Staff_BOD", Staff_BOD.SelectedDate.ToString("yyyy-MM-dd"));
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="Staff_Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Staff_Id" HeaderText="Staff_Id" InsertVisible="False" ReadOnly="True" SortExpression="Staff_Id" />
<asp:BoundField DataField="Staff_Name" HeaderText="Staff_Name" SortExpression="Staff_Name" />
<asp:BoundField DataField="Staff_UserName" HeaderText="Staff_UserName" SortExpression="Staff_UserName" />
<asp:BoundField DataField="Staff_Password" HeaderText="Staff_Password" SortExpression="Staff_Password" />
<asp:BoundField DataField="Staff_Gender" HeaderText="Staff_Gender" SortExpression="Staff_Gender" />
<asp:BoundField DataField="Staff_BOD" HeaderText="Staff_BOD" SortExpression="Staff_BOD"/>
<asp:BoundField DataField="Staff_Email" HeaderText="Staff_Email" SortExpression="Staff_Email" />
<asp:BoundField DataField="Staff_Address" HeaderText="Staff_Address" SortExpression="Staff_Address" />
<asp:BoundField DataField="Staff_Salary" HeaderText="Staff_Salary" SortExpression="Staff_Salary" />
<asp:BoundField DataField="Staff_Position" HeaderText="Staff_Position" SortExpression="Staff_Position" />
<asp:BoundField DataField="Staff_CNo" HeaderText="Staff_CNo" SortExpression="Staff_CNo" />
</Columns>
If your database column is a DateTime column, you don't need to format the date or cast it to a string before sending it as a parameter to the database.
Just use:
smdSelect.Parameters.Add("#Staff_BOD", Staff_BOD.SelectedDate);
I have the following GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="InvoiceID" HeaderText="SysInvoiceID" ReadOnly="True" SortExpression="SysInvoiceID" />
<asp:BoundField DataField="BillMonth" HeaderText="BillMonth" SortExpression="BillMonth" />
<asp:BoundField DataField="InvoiceDate" HeaderText="InvoiceDate" ReadOnly="True" SortExpression="InvoiceDate" />
<asp:BoundField DataField="InvoiceNumber" HeaderText="InvoiceNumber" SortExpression="InvoiceNumber" />
<asp:BoundField DataField="Net" HeaderText="Net" SortExpression="Net" />
<asp:BoundField DataField="VAT" HeaderText="VAT" SortExpression="VAT" />
<asp:BoundField DataField="Gross" HeaderText="Gross" SortExpression="Gross" />
<asp:ButtonField CommandName="ViewInvoice" HeaderText=" " ShowHeader="True" Text="View" />
</Columns>
</asp:GridView>
The very last column (ButtonField) is one I created myself just to include the text 'View' on each row, which when clicked, will bring up a PDF invoice.
I'm not sure if this is even possible, but I was wondering if it was possible to add some sort of validation for that column or something, so that if the 'InvoiceID' column is blank, the 'View' link on the corresponding row won't show up.
I felt close to doing this by going on split view in Visual Studio and then the 'Edit Columns' button on GridView tasks, but like I said I'm not sure if it's possible to do it this way and may have to resort to simply coding it.
Thanks for any help!
Use a <TemplateField> instead of a <ButtonField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="View"
Visible='<%# Eval("IsEmpty(InvoiceID)") %>' CommandName="ViewInvoice" />
</ItemTemplate>
</asp:TemplateField>
And add a method to your page that is IsEmpty(string id) or whatever type your id is, and just check to see if it's empty first.
You can also add a CommandArgument attribute to the Button that will let you specify what the argument to it will be.
Use a placeholder...
<asp:Placeholder runat="server" ID="plView" Visible="<%# Convert.ToBoolean(InvoiceID == null) ? false : true %>">
<asp:ButtonField CommandName="ViewInvoice" HeaderText=" " ShowHeader="True" Text="View" />
</asp:Placeholder>
Well it so happens that I am kind of a novice to asp.net and trying to create a Grid whose source is declared programmatically.
In the process, I came across 2 tags Fields and Columns. Can anyone please tell me how they are different?
EDIT: I went through some sample MSDN examples, and for all I can tell it seems to me they can be used interchangeably(though I have a feeling thats not true!).
Check this out:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="HireDate" HeaderText="HireDate"
SortExpression="HireDate" />
</Columns>
</asp:GridView>
And then there is:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateRows="False" DataKeyNames="ProductID"
DataSourceID="ObjectDataSource1" EnableViewState="False">
<Fields>
<asp:BoundField DataField="ProductName" HeaderText="Product"
SortExpression="ProductName" />
<asp:BoundField DataField="CategoryName"
HeaderText="Category"
ReadOnly="True" SortExpression="CategoryName" />
<asp:BoundField DataField="SupplierName"
HeaderText="Supplier"
ReadOnly="True" SortExpression="SupplierName" />
<asp:BoundField DataField="QuantityPerUnit"
HeaderText="Qty/Unit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice"
DataFormatString="{0:c}"
HeaderText="Price"
HtmlEncode="False" SortExpression="UnitPrice" />
</Fields>
</asp:GridView>
Seem similar or is it just me??!
Thanks for helping.
Columns is just the surrounding tag for the fields which are
TemplateFields with any controls you want or
BoundFields which are created automatically
So Columns enclose the list of fields in the GridView.
<Columns>
<asp:Boundfield datafield="StudentID"
readonly="true"
headertext="Student ID"/>
<asp:TemplateField HeaderText="Student" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:label runat="server" Font-Bold="true" ID="LblStudent" Text='<%# Bind("Student") %>'></asp:label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Inner Grid">
<ItemTemplate>
<asp:GridView ID="Grid2" AutoGenerateColumns="false" runat="server" GridLines="None" Width="300">
<RowStyle CssClass="GridViewRowStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<SelectedRowStyle BackColor="Aqua" />
<Columns>
<asp:TemplateField HeaderText="Student" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:label runat="server" Font-Bold="true" ID="LblStudent" Text='<%# Bind("Student") %>'></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
As you can see, a TemplateField could also contain another nested GridView.
Am I blind or wat!
After I posted this question I went back to my drawingboard and as it turns out, there is no Fields tag in asp:GridView, right?!
Please do let me know if this is true people(and prove me silly!)
Putting aside this particular control for a moment, it might be helpful to look at this from a general computer science point of view.
In classic programming (ANY LANGUAGE), a FIELD would be the INTERSECTION of a row and a column -- a discrete piece of data. For example, if a table has 20 rows of data containing first and last names, if you went to the 19th row and looked in the "first name" column, you've got a FIELD. Perhaps it contains the discrete data "JOHN".
COLUMNS then would be collections of like data -- in this example, you have the two columns "first name" and "last name". Columns would have attributes such as a data type, maximum length, constraints (are nulls OK?, etc.) and so forth.
Some may quibble with my definitions and say that individual cells in a COLUMN would be called a FIELD. It's not uncommon to hear that. I would reply that for a table with a single column, it'd be especially true :-) But the takeaway point is this: COLUMNS are generally containers for smaller, more discrete items such as FIELDS. FIELDS typically refer to a single piece of data, such as you'd find at the intersection of a row and column in a database table.
On my page there are textboxes which I use for input. How can I wire those textboxes to an EntityDataSource control?
Instead of using plain text boxes with your EntityDataSource control, I would recommend using a GridView control with a corresponding DetailsView control. The EntityDataSource control was designed to work nicely with these two controls. The DetailsView control will be the one which is actually responsible for the inserting and updating of your data.
When you are configuring the EntityDataSource controls (you will need two, one for the GridView and one for the DetailsView), make sure to check the 'Enable automatic inserts,' 'Enable automatic updates,' and 'Enable automatic deletes' check boxes in the 'Configure Data Selection' screen of the setup wizard. In your case, since you are only interested in inserting the data, you can just check the 'Enable automatic inserts' checkbox.
You can also just use the DetailsView by itself, and leave the GridView out. To do this, you'll need to configure the DetailsView's EntityDataSoruce 'Expression Editor' for the 'Where' clause a bit differently (found in the properties list of the EntityDataSource control).
It's important to point out that the GridView and DetailsView are pretty heavy controls. Unless you are building a small site (with few users) or prototyping a planned site project, I would recommend revising your development strategy and possibly going with raw ADO.Net instead.
Here is a simple example:
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=SomeDatabaseEntities"
DefaultContainerName="SomeDatabaseEntities"
EnableFlattening="False"
EntitySetName="Links" EnableDelete="True" EnableInsert="True"
EnableUpdate="True">
</asp:EntityDataSource>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
ForeColor="#333333"
GridLines="None" AllowPaging="True" AllowSorting="True"
EnableViewState="false"
DataSourceID="EntityDataSource1"
AutoGenerateColumns="False" PageSize="20"
DataKeyNames="LinkId">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="LinkId" HeaderText="LinkId"
SortExpression="LinkId" ReadOnly="True" />
<asp:BoundField DataField="Title" HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="URL" HeaderText="URL"
SortExpression="URL" />
<asp:BoundField DataField="ImagePath" HeaderText="ImagePath"
SortExpression="ImagePath" />
</Columns>
</asp:GridView>
<br />
<asp:EntityDataSource ID="EntityDataSource2" runat="server"
ConnectionString="name=SomeDatabaseEntities"
DefaultContainerName="SomeDatabaseEntities"
EnableFlattening="False"
EntitySetName="Links" Where="it.LinkId == #LinkId"
EnableInsert="True"
EnableUpdate="True">
<WhereParameters>
<asp:ControlParameter ControlID="GridView1"
DbType="Int32" Name="LinkId"
PropertyName="SelectedValue" />
</WhereParameters>
</asp:EntityDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px"
Width="125px"
CellPadding="4" ForeColor="#333333"
GridLines="None" DataKeyNames="LinkId"
AutoGenerateRows="False"
DataSourceID="EntityDataSource2">
<Fields>
<asp:BoundField DataField="LinkId"
HeaderText="LinkId"
ReadOnly="true" InsertVisible="false"
SortExpression="LinkId" />
<asp:BoundField DataField="Title"
HeaderText="Title"
SortExpression="Title" />
<asp:BoundField DataField="URL" HeaderText="URL"
SortExpression="URL" />
<asp:BoundField DataField="ImagePath"
HeaderText="ImagePath"
SortExpression="ImagePath" />
<asp:CommandField ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>