Put multiple rows of a gridview into edit mode - c#

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.

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.

Adding hyperlinks to labels in certain cases, when dealing with databound repeater

Sounds a bit cluttered, but basically I have a databound repeater. On the ASP side, I have this:
<asp:Label ID="Label2" runat="server" Text='<%#Eval("uMessage") %>'></asp:Label>
I'm using the same template for 4 different datasets, and for 2 of them this should be a hyperlink and for the other 2 it shouldn't. So, I'm guessing you have to add a hyperlink programmatically in the code-behind? Has anyone ever done something like this?
Easiest way without all kinds of code-behind and therefor less code fragmentation, I would say you need a property that is set based on your condition prior to data binding.
protected bool LinkVisible { get; set; }
Then you just do this:
<asp:Label ID="Label2" runat="server" Text='<%#Eval("uMessage") %>' Visible="<%# !LinkVisible %>"></asp:Label>
<asp:HyperLink ID="Link" runat="server" Visible="<%# LinkVisible %>" ><%#Eval("uMessage") %></asp:HyperLink>
This sets the Visible for either the Label or the HyperLink. Visible false means it won't even get rendered. In your markup you can see that there will be a label or a hyperlink and no special things popup from the code behind.
You don't need to add the property LinkVisible, but can do the condition there too.
yes it is possible in code behind on DataItem bound
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label)e.Row.FindControl("Label2");
if (lbl.Text == "your condition")
{
HyperLink yourLink = (HyperLink)e.Row.FindControl("yourID");
yourLink.enabled = false;
}
}

Disable a TextBox in GridView and retain its value c#

I have a TextBox in a GridView which takes a user input. The problem is that it loses its value when I click on a Button placed in the GridView footer which adds an empty row to the GridView,How can I fix it?
Also, this happens only when I set its Disabled attribute to false in RowDataBound event. But if I set the ReadOnly attribute, everything works fine.
This is the TextBox in GridView.Bound to a DB data:
<asp:TemplateField HeaderText="Shift Code">
<ItemTemplate>
<asp:TextBox runat="server" CssClass="txtingrid" ID="lblShiftCode" Text='<%# Eval("SHIFTCODE") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
this is what I do in the RowDataBound:
if (e.Row.RowType == DataControlRowType.DataRow)
{
lblShiftCode.Attributes.Add("disabled", "true");
}
Please help.:)
lblShiftCode.Attributes.Add("disabled", "disabled");
If you are doing a post back on the button click, you will have to set the UseSubmitBehavior attribute to "false" on the button to retain values of the disabled controls.
More info here
Its very simple just rebind the gridview after clicking the button. Suppose you are using gridbind(); method to bind your gridview.So you have to call this method again after button click.

.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.

asp.net bind a list to a gridview

List<MasterBook> listOfBooks = new List<MasterBook>();
after i put the masterbook objects which by the way have 3 fields( name,id and active ) into the list
GridView1.DataSource = listOfBooks;
GridView1.DataBind();
in the web form
<Columns>
<asp:TemplateField HeaderText="Book Name">
<ItemTemplate>
<asp:Label ID="BookNameText" runat="server"
Text="<%#Container.DataItem%>">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
the problem is that i want to display the bookmaster's name in the gridview column
this code print bookmaster in the column how can i make it type the name
Change:
<%#Container.DataItem%>
To:
<%#Eval("name")%>
Cast it to MasterBook:
Text="<%# ((MasterBook) Container.DataItem).Name %>">
The quick, dirty hack workaround is to overload the ToString() method of your MasterBookclass in order to return that name. Commit seppuku afterwards if you do it.
The elegant and graceful way is to make an object datasource. That way you can bind the grid view's columns to the objects' properties. In the case of a DataList or other templated data controls, you can bind your objects' properties to the data control's child controls in their templates.
The middle path is in hutchonoid's answer: evaluate a property of the object, not the object itself.
Use this sintax:
<asp:Label ID="BookNameText" runat="server"
Text="<%# ((MasterBook)Container.DataItem).Name %>">

Categories