Need to make a specific column editable upon clicking edit - c#

I have a gridview with "Edit Update Cancel" command field.
When I click Edit, all the columns in the particular row becomes editable.
I just need to have 2 specific columns editable. How is that made possible ?
(Screen Shot Attached)
[In the screen shot all 3 columns are editable, I just need the second and third to be editable]
Thanks in Advance.

Just set the other columsn to read-only:
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" />

It is very easy. The column you want not editable then place just label and bind properly.
<EditItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="Label1"></asp:Label>
</EditItemTemplate>
Here I use label in my EditItemTemplate because when the user clicks on edit button, the textbox will not come up. Rather, the value for that record will show through label, and as a result the user can not update that field.

Related

Set Focus for ItemTemplate textbox in grid view for asp.net c#

I have one query which is related to get focus in itemfield of Textbox in gridview. Based on checkbox selection populating different number of textBoxes and after entered data in last textbox and making webservice call based on response populating gridview with itemField(textboxes).
The scenario is explaining below.
CheckBox(Unchecked): False
Number of text boxes(ID): 1
CheckBox(Checked): True
Number of text boxes(ID, AccNo, Price): 3
After entered values in last textboxes, make webservice call and filling gridview with data but it's not get focus of first textbox in GridView.
<asp:TemplateField>
<HeaderTemplate>
<p>Paid Amount</p>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAmnt" runat="server" TabIndex="1"
onkeypress="return isNumberKey(event,this)" onblur="calAmount(event,this)" Text='<%# Eval("PaidAmount") %>'>
</asp:TextBox>
</ItemTemplate>
I have not any event related gridview making like, rowBound, CommandView. Could you please show me a way related this.
Thank you so much in advance.

Recording clicked Grid View column

I have a grid view in a asp page which have a hyperlink field,say,CustomerID which navigates user to various webpages. The value of the CustomerID in the grid view can have duplicate values. So, what I want is how to record all the clicked values in the CustomerID or the hyperlink?
Please suggest solution such that the solution is viable for multiple hyperlink column in gridview each navigation to different webpage.
You can use combination of gridview bind columns and pass them as parameters in url and retrieve in other page as Request.QueryString[] collection.
Eg
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" NavigateUrl='<%# string.Format("~/Details.aspx?CustomerID={0}&Name={1}&Country={2}",
HttpUtility.UrlEncode(Eval("CustomerID").ToString()), HttpUtility.UrlEncode(Eval("Name").ToString()), HttpUtility.UrlEncode(Eval("Country").ToString())) %>'
Text="View Details" />
</ItemTemplate>
</asp:TemplateField>

.NET framework 4.5 loads default edit template on GridView?

I've created a gridview with edit feature. On clicking the gridview row edit image, it makes the custom edit panel visible and the user can edit and save the gridview row data or s/he can click the cancel button without any updates. It works perfectly fine with .NET framwork 2.0.
But, the problem lies with the .NET framework 4.5 -
when the user clicks the cancel or save button, instead of loading the
normal gridview, it loads the default edit template on the top of
gridview row where the user had clicked before.
So, how can I solve this problem? This happens in all the Gridviews used in our ASP.NET website.
Here's the original Gridview Row: Original GridRow
Here's the custom edit panel with save/cancel: Custom Edit Panel
Here's error: Default Template loaded
I usually achieve what you are trying to do in the row databound event.
Try setting up your grid view like this
<asp:Gridview id="ClientManagementGridView runat="server"/>
<columns>
<asp:TemplateField>
<itemtemplate>
//put edit button here
</itemtemplate>
<edititemtemplate>
// put your update and cancel buttons here
</edititemtemplate>
</asp:Templatefield>
<asp:templatefield>
<itemtemplate>
<aspTextBox Text='<#Eval("ClientId") %>' runat="server" />
</itemtemplate>
<edititemtemplate>
<asp:Panel id="EditPanel">
//your panel contents
</asp:Panel>
</edititemtemplate>
</asp:templateField>
<asp:BoundField DataField="ClientName" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Name" />
</columns>
<asp:GridView>
Now in your row data bound event of your gridview put this magic piece of code
If e.Row.RowState.ToString().Contains("Edit") Then
Dim editGrid As GridView = TryCast(sender, GridView)
Dim colSpan As Integer = editGrid.Columns.Count
e.Row.Cells(0).Visible = True
e.Row.Cells(1).Visible = True
For i As Integer = 2 To colSpan - 1
e.Row.Cells(i).Visible = False
e.Row.Cells(i).Controls.Clear()
Next
e.Row.Cells(1).Attributes("ColSpan") = (colSpan).ToString()
End if
As you can see I am overriding the default edit item template by clearing its default controls and then changing the column span of the cell to fit my custom edit panel.As well as hiding cells I want to be hidden.

Binding a Button to a GridView

This a pretty simple question, I'm just not sure how to do it exactly. I would like to bind a Button or perhaps ImageButton to a GridView in ASP.NET/C#. Currently, the GridView has two columns and is bound to a DataTable with two columns. I want to add a third column to the GridView, which will include the Button.
I know GridView has ButtonField, but I'm not too sure how to go about using it to do what I want. I want to dynamically generate these Buttons and add them to the GridView.
Here is how my GridView looks right now:
<asp:GridView
ID="GridView1"
Runat="server">
<Columns>
<asp:HyperLinkField
HeaderText="Display Name"
DataNavigateUrlFields="DISPNAME"
DataNavigateUrlFormatString="ViewItem.aspx"
DataTextField="DISPNAME">
<ItemStyle Width="70%" />
</asp:HyperLinkField>
<asp:BoundField
DataField="TypeDisp"
HeaderText="Type">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
You can use a template field like the following,
<TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="image url" CommandName="SomeCommand" CommandArgument='<%# Eval("Id") %>'/>
</ItemTemplate>
</TemplateField>
Then you can handle the RowCommand event of the GridView and check the e.CommandName to see what command to be executed and you can get the e.CommandArgument as well which could be the row Id like I used in the code above.
If we are talking a button that's always present, you can use ButtonField, or even use a TemplateField and provide the template with the button, and bind the data to the button (sounds like you may want to bind data to the attributes of the button?)
If you are looking to dynamically generate buttons in the UI, tap into the RowCreated event and add the button the GridView. You'd have to do this on every page load; the GridView won't remember a button created programmatically.
HTH.

Put multiple rows of a gridview into edit mode

I have the need to allow a user to "tab through" making edits on a gridview. There will be one editable column in the row data. The user should be able to hit tab and go to the next row to edit said column.
I have not found any easy method to accomplish this task. I found a way to programmatically put a gridview into edit mode, but in testing the code below it works for only 1 row at a time.
reviewTransferGV.EditIndex = 0;
reviewTransferGV.Rows[0].RowState = DataControlRowState.Edit;
reviewTransferGV.EditIndex = 1;
reviewTransferGV.Rows[1].RowState = DataControlRowState.Edit;
reviewTransferGV.DataBind();
I did a workaround by creating a property in the page:
protected bool IsEditMode
{
get { return this.EditMode; }
set { this.EditMode = value; }
}
Then in the GridView I have the controls for view and edit mode inside an item template. Setting the visibility based on the property value:
<asp:TemplateField SortExpression="Status" HeaderText="Status">
<ItemTemplate>
<asp:Label Id="lblStatus" Text='<%# Eval("Status") %>' Visible='<%# !IsEditMode %>' runat="server" />
<asp:TextBox ID="txtStatus" Text='<%# Eval("Status") %>' Visible='<%# IsEditMode %>' runat="server" />
</ItemTemplate>
This works for editing the whole gridview. You'll probably need to make a few modifications to make it work for individual rows.
I don't believe it is possible for a GridView to have multiple rows in edit mode simultaneously. If you want to edit multiple rows, you will need to roll your own mechanism to do so.
Another point is how to save the results to the DataBase.
While in regular use, we simple call the update command who does the work, in ItemTemplate there are now update button.
So i add a button outside the GridView and in the handler i call the UpdateRow method manually for each row.

Categories