I am using a gridview Edit to edit the values i have in my gridview, when i press edit, all columns can be edited, i would like that one of the columns is not allowed to be edited.
Is there any way i can do this?
Thiss is my aspx code:
<asp:GridView
ID="GridView1"
runat="server"
AllowSorting="True"
OnRowCommand="GridView1_SelectedIndexChanged1"
AutoGenerateEditButton="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
CellSpacing="10"
OnRowUpdating="GridView1_RowUpdating" ShowFooter="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing">
</asp:GridView>
Thanks
If you are using asp:BoundField, try
<asp:BoundField DataField="CustomerID"
ReadOnly="true"
HeaderText="Customer ID"/>
Or else if you are using asp:TemplateField, you can either
Render it in either an asp:Label inside EditItemTemplate
Omit the EditItemTemplate for that column altogether
Sure, make use of the EditItemTemplate. In the following example field ID will not be edited in the Edit mode:
<asp:GridView runat="server">
<Columns>
...
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<%# Eval("ID") %>
</EditItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
please show some markup. Quick and dirty I think depending on your markup aspx you can remove the textbox from the EditItem template for the column you want to prevent editing... there are also other solutions of course :)
If you are using template field
((TemplateField)gvGridView.Columns[index]).EditItemTemplate = null;
if boundfield
((BoundField)gvGridView.Columns[index]).ReadOnly = true;
Related
I use GridView to display data from DB. First I query data from DB to list<list<string>>, sort use linq, copy this array to DataTable and bind DataTable to GridView. But if in DataTable more then 1000 rows - it works very long or not works (error in browser).
How to fix this?
UPDATE i use rowspan in columns and create a delete buttons to all rows, and if i use paging wiil it works?
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnDataBinding="GridView1_DataBinding" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="Number" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Name" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Nameoid" ItemStyle-Width="200px" > <ItemStyle Width="200px" > </ItemStyle>
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return DeleteConfirm();" OnClick="Button2_Click" />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%#Bind("Number") %>' />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It seems like you are doing lots of server side code in one round trip time. You should make it lesser so that page could show the result in grid view.
Here are few things on which you should work:
There is no need to store first in list then data table and then sorting. You can directly use the DATAVIEW class to sort the result from DB.
I think you should not display all 1000 record in grid view in a single page. You should use PAGING in grid view. This is also in built functionality of grid view by this you can show number of record in a one grid view page as you want..
For reference how to do this there is complete example. Follow the below link
http://www.dotnetgallery.com/kb/resource12-How-to-implement-paging-and-sorting-in-aspnet-Gridview-control.aspx
UPDATE:
If you want to delete a row in gridview with a link button on each row. Then follow the below article
enter link description here
You can put the UpdatePanel outside of gridview. First try to use the above code (from link), then try to add update panel. If you feel any difficulties ask here.
My suggestion would be to use Lazy loading pattern with your GridView .
You can refer to :
LazyLoadUpdatePanelusingTimerControlAJAX
or you can implement custom paging
Custompaging
You can use paging in your grid view having grid size of say 10,20,30 to accommodate large amount of data so that the page doesn't crash.
Try referring to this example
http://www.codeproject.com/Articles/106678/Display-Large-Amount-of-Data-in-GridView-with-Sear
I'm still learning asp.net and I want to ask you about how to reverse the grid view, I mean the default grid view would be like this
and this is the code for it:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True"
AutoGenerateColumns="false" Width="256px">
<Columns>
<asp:TemplateField HeaderText="1"> <ItemTemplate>
<asp:Label ID="test1" runat="server" Text="test1"></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="2"> <ItemTemplate>
<asp:Label ID="test2" runat="server" Text="test2"></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="3"> <ItemTemplate>
<asp:Label ID="test3" runat="server" Text="test3"></asp:Label>
</ItemTemplate></asp:TemplateField> </Columns>
<RowStyle HorizontalAlign="Left" VerticalAlign="Bottom" />
</asp:GridView>
and i want it to be like this picture :
the test here will be data bound and the numbers should be header text, this is only for test, is there a way to reverse it?
thank you all for your valuable advises and efforts, I really appreciate it.
I was able to achieve what I want by pivoting the data table and the columns became rows and the opposite.
this link can explain it.
thx
i have a gridview with AutoGenerateDeleteButton Property set true.
Of course this property auto generates a linkbutton at the leftmost of the gridview, my question is, how can i change it to an Image Button?? i wanted my gridview to look more presentable by making the control buttons an image.
Thanks! :)
You can do by using template in grid...
for more info
http://p2p.wrox.com/asp-net-2-0-professional/46216-image-button-template-field-gridview-control.html
You can create a TemplateField and use autogeneratecolumns="false".
Here's an example of a GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Link" runat="server" Text="click" OnClick="link_Click"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="field1" HeaderText="My Column 1" />
<asp:BoundField DataField="field2" HeaderText="My Column 2" />
</Columns>
</asp:GridView>
Where field1 and field2 are headers from your DataTable
And to access the row within the event handler:
protected void link_Click(object sender, EventArgs e)
{
int rowindex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
//do something with rowindex etc
}
Add new Template Column and in add image button and set CommandName='Delete' it'll raise the delete event automatically.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgDelete" ImageUrl="~/imgs/Delete.png"
CommandName="Delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Try this
<Columns>
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/DeleteImage.png"
ShowDeleteButton="true"/>
</Columns>
And set AutoGenerateDeleteButton="false"
Or Refer this too
I need to hide content of a column based on comparing its data with a string. I like to do it in Page itself (Page does not have code behind)
For some reason I cannot use Eval or Bind to retrieve data for column. I'm looking for something like,
<asp:GridView ID="GridView1" runat="server" >
<Columns>
<asp:TemplateField>
<%
if ([data from row] == aVarContainingDataToCompare){
Response.Write("Hidden");
} else {
Response.Write([data from row]);
}
%>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate><%# Eval("AnotherData") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Can I do this without using code behind
Is it alright to use following instead? Please note the # sign at the start
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<%# (Eval("TheColumn").ToString() == aVarContainingDataToCompare ? "Hidden": Eval("TheColumn")) %>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
If I have this <asp:ButtonField runat="server" DataTextField="Name" CommandName="GetName"></asp:ButonField> Within a GridView. Is there any way to retrieve the DataTextField (Name) from the OnRowCommand method?
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource_Names"
DataKeyNames="ID,ModuleId" OnRowCommand="ChangeName">
Or alternatively, is there any way to make CommandName into an attribute where the command is dynamically entered based on given data, similar to the difference between DataTextField vs TextField.
I added <asp:BoundField DataField="Name" /> Then you can retrieve it using the same method here: GridView.onRowCommand Method
All I need to do now is find out a way to hide the field. If anyone knows how to, please let me know. Visible="false" gets rid of the field all together...
Not really sure if this is the best way, but it works:
Make your column a templatefield and bind the name value to a asp:hiddenfield control:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfName" runat="server" Value='<%# Eval("Name") %>'>
</asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>