Allowing one column to be edited but not another - c#

i have an asp.net c# application.
my gridview has a datasource that has 2 fields.
1 field cannot be edited by the user, but i need the other one to be editable!
is this possible to do?

Set the ReadOnly="true" property on all that you don't want editable.
Take a look at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.readonly.aspx
A quick example from that page
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
autogenerateeditbutton="true"
allowpaging="true"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:boundfield datafield="CustomerID" readonly="true" headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName" readonly="true" headertext="Customer Name"/>
<asp:boundfield datafield="Address" headertext="Address"/>
<asp:boundfield datafield="City" headertext="City"/>
<asp:boundfield datafield="PostalCode" headertext="ZIP Code"/>
</columns>
</asp:gridview>
In this case CustomerID and Company name are read only and can't change. Address, City and PostalCode can be edited.
Just set the ReadOnly option to true on the columns you DON'T want people to edit. The columns that don't have this set or have ReadOnly set to false are editable by the user when in edit mode.

If you use a SqlDataSource then you should make sure to remove the columns to update from the UpdateCommand in the SqlDataSource if not you will have a problem when you update the fields

You should be able to set ReadOnly on DataGridViewCell.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.readonly.aspx
gridView.Rows[rowIndex][colName].ReadOnly = true;

Related

Relationship one to one in GridView in asp.net C# application

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.

How to filter Gridview columns on the basis of column type

How to filter Gridview columns on the basis of column type(i.e. Bound field , template field etc...)
Suppose i have a gridview with following code
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
runat="server">
<columns>
<asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName" headertext="Company Name"/>
<asp:boundfield datafield="Address" headertext="Address"/>
<asp:boundfield datafield="City" headertext="City"/>
<asp:boundfield datafield="PostalCode" headertext="Postal Code"/>
<asp:boundfield datafield="Country" headertext="Country"/>
<asp:templatefield headertext="Author Name">
<itemtemplate>
<asp:label id="FirstNameLabel"
Text= '<%# Eval("au_fname") %>'
runat="server"/>
<asp:label id="LastNameLabel"
Text= '<%# Eval("au_lname") %>'
runat="server"/>
</itemtemplate>
</asp:templatefield>
<asp:hyperlinkfield text="Details..."
navigateurl="~\details.aspx"
headertext="Order Details"
target="_blank" />
</columns>
</asp:gridview>
Now i want to run through all the boundfields only (so last 2 columns should be filtered out).
I know it can be done by going through all the columns of a gridview and checking column type(with grdView.Columns[ct].GetType().Name;) but in my actual scenario it may be having 25 - 30 columns so dnt want to run through all columns but want to filter out only boundfileds first and then run through them
I dont want run a loop through all the available columns ..
Well, to filter columns, you have to look at each of them to check whether the column satisfies your condition. So you have to loop over all of them in any case, it can be explicit, or implicit, but it is there.
You can use Linq for something like that,
var filtered = CustomerGridView.Columns.Where(
column => column.GetType().Name == "Some column type" );
It returns an IEnumerable (actually, a lazy generator behind the scenes), which only gets evaluated when you actually iterate over it.

Databind to gridview from backend c#, but show the fields only which is mentioned in BoundField of gridview

I am using the linq to fetch from database and bind in the gridview using the below code:
details.aspx.cs
var mlo1 = (from nmo2 in nmo.PrimaCustDetails1s select nmo2).ToList();
custdet.DataSource = mlo1;
custdet.DataBind();
details.aspx
<asp:GridView ID="custdet" runat="server">
</asp:GridView>
All the data from database will be shown.
I want to show only some fields from the table using Boundfield
<asp:GridView ID="custdet" runat="server">
<Columns>
<asp:BoundField DataField="CustAccNo" HeaderText ="AccNo" />
<asp:BoundField DataField="Name" HeaderText="Customer Name" />
</Columns>
</asp:GridView>
I don't want other fields... Can someone advise me on how to do this?
Set Gridview property AutoGenerateColumns="false"
<asp:GridView ID="custdet" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="CustAccNo" HeaderText ="AccNo" />
<asp:BoundField DataField="Name" HeaderText="Customer Name" />
</Columns>
</asp:GridView>
Try adding the AutoGenerateColumns="false" attribute to the asp:GridView element
You can set autogenerate property of gridview columns to false Gridview Autogeneratecolumns Property
<asp:GridView AutoGenerateColumns="False" />

AutoGenerateInsertButton in Gridview

I have the following GridView
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
OnRowCommand="GridView1_RowCommand" DataKeyNames="Chart_Id"
AutoGenerateColumns="False" EnableModelValidation="True" >
<Columns>
<asp:CommandField ShowEditButton="False" ShowDeleteButton="False" ShowInsertButton="False" />
<asp:BoundField DataField="Week" HeaderText="Week" SortExpression="Week" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" ItemStyle-Wrap="False" />
<asp:BoundField DataField="Host" HeaderText="Host" SortExpression="Host" />
<asp:BoundField DataField="Topic_1" HeaderText="Topic 1" SortExpression="Topic_1" />
<asp:BoundField DataField="Topic_2" HeaderText="Topic 2" SortExpression="Topic_2"
HeaderStyle-Wrap="False" />
<asp:BoundField DataField="Topic_3" HeaderText="Topic 3" SortExpression="Topic_3" />
<asp:BoundField DataField="Topic_4" HeaderText="Topic 4" SortExpression="Topic_4" />
</Columns>
</asp:GridView>
By default, I have the edit/insert/cancel buttons set to false.
Then in the code behind, I want to be able to set these to true during certain conditions.
string theUser = Helpers.GetUser();
string admin = "adminName";
if (theUser == admin) {
// Set the buttons to true
}
I've been looking for ways to do this, and someone suggested to use the AutoGenerate properties, and then enable them like so:
GridView1.AutoGenerateEditButton = true;
GridView1.AutoGenerateDeleteButton = true;
GridView1.AutoGenerateInsertButton = true; // This one throws an error
Only problem is, AutogenerateInsertButton does not seem to exist, in the main ASPX page or in the code behind.
Can anyone suggest some ways for me to access these properties and set them to true?
Thank you.
Why do you think that a GridView should have an AutoGenerateInsertButton property?
A GridView is a list of GridViewRows, where each row represents a record/element/item which can be edited or deleted. But it doesn't make sense to have a insert-button for each record because it already exists.
You could follow this tutorial which shows how to use the footer-row of the GridView to insert a new record.
The property AutoGenerateInsertButton exists on the DetailsView control. Whoever designed the control probably figured that you don't need an insert button for each row in the grid, since each would essentially do the same thing.
So, maybe you could display an empty DetailsView at the bottom of the grid, or just create your own insert command using a regular Button.

Difference between <Fields> and <Columns> in <asp:GridView>

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.

Categories

Resources