I am using GridView for Showing Some Data with Editable Format. But When I am Editing the GridView row I don't get the Value of editable Textbox. Again I get the old Value.Please any one give some Suggestion.
Thanks in Advance...
Aspx Code:
<asp:GridView ID="grdAppSetting" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" HeaderStyle-BackColor="#1abc9c" HeaderStyle-ForeColor="White" CssClass="t-transform m-t-30" HeaderStyle-BorderWidth="5px"
runat="server" OnRowCancelingEdit="grdAppSetting_RowCancelingEdit1" OnRowDeleting="grdAppSetting_RowDeleting" OnRowUpdating="grdAppSetting_RowUpdating"
OnRowEditing="grdAppSetting_RowEditing" AutoGenerateColumns="false" BorderColor="#1abc9c">
<Columns>
<asp:BoundField DataField="Key Name" HeaderText="Key Name" ReadOnly="true" ItemStyle-CssClass="p-l-10" ItemStyle-Width="100" />
<asp:BoundField DataField="Value" HeaderText="Value" ItemStyle-Height="40px" ItemStyle-Width="50%" ItemStyle-CssClass="p-l-10" ControlStyle-CssClass="form-control" />
</Columns>
</asp:GridView>
C# Code:
protected void grdAppSetting_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Value = ((grdAppSetting.Rows[e.RowIndex].Cells[2].Controls[0]) as TextBox).Text;
string KeyName = grdAppSetting.Rows[e.RowIndex].Cells[1].Text;
if (appSettingsSection != null)
{
appSettingsSection.Settings[KeyName].Value = Value;
configuration.Save();
}
grdAppSetting.EditIndex = -1;
BindAppSettingGrid();
}
Problem occurred in the following line,
string Value = ((grdAppSetting.Rows[e.RowIndex].Cells[2].Controls[0]) as TextBox).Text;
Is this is correct to get the editable textbox value???
Related
I'm currently working on an in house project, I've created a GridView connected to a SQL table which looks like this:
GridView1
I created the view content buttons using the following code:
<Columns>
<asp:ButtonField ButtonType="Button" Text="View Content" CommandName="Select" />
<asp:BoundField DataField="ContentID" HeaderText="ContentID" InsertVisible="False" ReadOnly="True" SortExpression="ContentID" />
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="URL" HeaderText="URL" Visible="true" SortExpression="URL" />
</Columns>
But this is where I am now stuck. I would like to click on the view content button and have it navigate to the URL on the selected row.
The URL comes from the SQL Table and there is a string so I'd imagine, it would need to be converted first but I could be wrong.
I started to put my code in the following:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewCommandEventArgs x = (GridViewCommandEventArgs)e;
if (x.CommandName == "Select")
{
GridViewRow row = GridView1.SelectedRow;
}
}
Add your code in GridView1_RowCommand event of gridview:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
int index = row.RowIndex;
string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
Response.Redirect(url); // url can be from your sql table
}
}
Note: Don't forget to add OnRowCommand event in GrindView <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" >
GridViewRow row = (GridViewRow)(((BoundField)e.CommandSource).NamingContainer);
int index = row.RowIndex;
string url = (GridView1.Rows[index].FindControl("URL") as BoundField).Text;
Response.Redirect(url); // url can be from your sql table
So i made the change. Unfortunately, BoundField does not contain a definition for NamingContainer.
Also, GridView1.Rows[index].FindControl("URL") as BoundField fails due to the following error, cannot convert type 'system.web.ui.control' to 'system.web.ui.webcontrols.boundfield' via a reference conversion, boxing conversion, unboxing conversion or null type conversion.
We have a webform with a radgrid and a button. The radgrid has two columns and a checkbox column.
On Page_load, the data will be displayed and the user will be able to select (via the checkbox) which rows will go through with the update of the EmpId; if the row is selected, NewEmpId will replace OldEmpId. The Update button is clicked and the changes are made.
The problem I'm having is that I prefer to use the datakeynames to retrieve the data from each row. But I'm having trouble implementing it without using GridCheckBoxColumn, which I don't want to use because the radgrid needs to be in Edit mode for the checkbox to be selected, and that brings other issues.
<telerik:RadGrid ID="RadGridEmp" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True" PageSize="22">
<ClientSettings>
<Selecting AllowRowSelect="True" />
</ClientSettings>
<mastertableview commanditemdisplay="TopAndBottom" datakeynames="OldEmpId, NewEmpId, EmpName">
<commanditemsettings showaddnewrecordbutton="false" />
<Columns>
<telerik:GridBoundColumn DataField="OldEmpId" HeaderText="OldEmpId">
<HeaderStyle Width="110px"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="NewEmpId" HeaderText="NewEmpId">
<HeaderStyle Width="110px"></HeaderStyle>
</telerik:GridBoundColumn>
<ItemTemplate>
<asp:CheckBox ID="ChkChange" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</mastertableview>
</telerik:RadGrid>
The following code is just the code I prefer to use with this issue. It uses datakeynames. But it doesn't build:
protected void RadButtonUpdateSectors_Click(object sender, EventArgs e)
{
foreach (GridItem item in RadGridEmp.MasterTableView.Items)
{
GridDataItem dataitem = (GridDataItem)item;
TableCell cell = dataitem["GridCheckBoxColumn"];
CheckBox checkBox = (CheckBox)cell.Controls[0];
if (checkBox.Checked)
{
oldEmpId = dataitem.GetDataKeyValue("OldEmpId").ToString();
newEmpId = dataitem.GetDataKeyValue("NewEmpId").ToString();
UpdateEmpId(oldEmpId, newEmpId, connString);
}
}
}
Please try with the below code snippet.
datakeynames="OldEmpId,NewEmpId,EmpName"
...............
...............
protected void RadButtonUpdateSectors_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGridEmp.MasterTableView.Items)
{
CheckBox ChkChange = item.FindControl("ChkChange") as CheckBox;
if (ChkChange.Checked)
{
string oldEmpId = item.GetDataKeyValue("OldEmpId").ToString();
string newEmpId = item.GetDataKeyValue("NewEmpId").ToString();
UpdateEmpId(oldEmpId, newEmpId, connString);
}
}
}
Let me know if any concern.
Note:- Remove extra space from datakeynames.
I want to display data in gridview based on check box selection from another grid view. The given below code getting values into from first gridview based on check box selection. I want to bind that values into second grid. Help me to find a proper solution. Thank you.
Code :
protected void btnAssign_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
if (chkRow.Checked)
{
string[] EmpId = new string[] { row.Cells[0].Text };
string[] EmpName = new string[] { row.Cells[1].Text};
// I want to display emp id and emp name on gridview 2 based on check box selection. How can I do. Help me to find a proper solution
GridView2.DataSource = EmpId;
GridView2.DataBind();
}
}
}
}
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="Employee Name" ReadOnly="True" SortExpression="FirstName" />
<asp:TemplateField HeaderText="Select" SortExpression="Select">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server"> </asp:GridView>
The given below image is showing data in grid view based on drop-down selection. After that I want to select some rows and click assign then I have to display the selected rows in new gridview.
Image :
If you don't already have a strong type defined to represent your selected Employee, you could create a Dictionary to store your selected employees. Upon completing your iteration over GridView1's rows to get the selected employees, you could use the dictionary as the datasource for GridView2 and bind to the Key and Value of each entry.
protected void btnAssign_Click(object sender, EventArgs e)
{
Dictionary<int, string> selectedEmployees = new Dictionary<int, string>();
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
if (chkRow.Checked)
{
selectedEmployees.Add(int.Parse(row.Cells[0].Text), row.Cells[1].Text);
}
}
}
if (selectedEmployees.Any())
{
gridView2.DataSource = selectedEmployees;
gridView2.DataBind();
}
}
UPDATE: Updated the code to account for the exception that you were receiving with the sample code provided
I have a gridview being filled from a SQL data source.
Whenever I open the page I get a stackoverflow exception in the gridview rowdatabound.
What is causing the problem?
Search.aspx:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ConsultsSQLDataSource" ForeColor="#333333" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="DATA" HeaderText="Data" SortExpression="DATA" />
<asp:BoundField DataField="LOCAL" HeaderText="Local" SortExpression="LOCAL" />
<asp:BoundField DataField="URGENCIA" HeaderText="Urgencia" SortExpression="URGENCIA" />
<asp:BoundField DataField="ESTADO" HeaderText="Estado" SortExpression="ESTADO" />
<asp:HyperLinkField HeaderText="Pagamento" NavigateUrl="a" Text="Link" Visible="False" />
<asp:BoundField DataField="IDPAGAMENTO" SortExpression="IDPAGAMENTO" Visible="False" />
</Columns>
</asp:GridView>
Code-Behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[2].Text;
switch(value)
{
case "3":
e.Row.Cells[3].Text = "Waiting Payment";
HyperLinkField hp = (HyperLinkField)GridView1.Columns[4];
GridView1.Columns[4].Visible = true;
GridView1.Columns[5].Visible = true;
hp.NavigateUrl = "~/Account/Payments/Payment?PaymentID=" + e.Row.Cells[5].Text; //Exception occurs here
hp.Text = "Pay";
e.Row.Cells[4].Visible = true;
break;
}
}
}
I've noticed that if you assign NavigateUrl and Text to hyperlink by referencing HyperLinkFiled like you do (GridView1.Columns[4]) it isn't assigned to current row but to the next row which doesn't seem to be what you expect.
Rebuild your RowDataBound method like that:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[2].Text;
switch (value)
{
case "3":
e.Row.Cells[3].Text = "Waiting Payment";
HyperLink hp = e.Row.Cells[4].Controls[0] as HyperLink;
hp.NavigateUrl = "~/Account/Payments/Payment?PaymentID=" + e.Row.Cells[5].Text;
hp.Text = "Pay";
break;
}
}
}
and remove visible="false" from HyperLinkField and last BoundField in grid markup.
You can remove Text and NavigateUrl properties from HyperLinkField so you only show content in the cell if there is correct link.
Try it and see if you still getting error.
In reference to Link loaded into my gridview try to navigate to my local server. The columns I have in the datagrid are Customer #, Description, Link.
I've got a function set up that's called on rowDataBound, but how do I retrieve what link is in the row so that I can edit it, and then rebind it to the datagrid?
protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
{
if ( e.Row.RowIndex == 2 )
{
}
}
And here is my gridview code
<asp:GridView ID="grdLinks" runat="server" AutoGenerateColumns="False" DataSourceID="ldsCustomerLinks"
OnRowDataBound="grdLinks_RowDataBound" EmptyDataText="No data was returned."
DataKeyNames="ID" OnRowDeleted="grdLinks_RowDeleted" Width="80%" BackColor="White"
HorizontalAlign="Center" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:HyperLinkField DataTextField="Link" HeaderText="Link" SortExpression="Link" DataNavigateUrlFields="Link" Target="_blank" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="ldsCustomerLinks" runat="server" ContextTypeName="ComplianceDataContext"
TableName="CustomerLinks" EnableDelete="true">
</asp:LinqDataSource>
If I'm understanding you correctly, you want to get the value of a data item called Link. If so, then something like this should work:
EDIT: I think what you're saying is that you want to grab the value Link from the database, manipulate it and then set the Url of the HyperLink to the new, manipulated value, if so then it would look like this:
ASPX Page (Updated to reflect posted code)
<Columns>
<asp:BoundField DataField="CustomerNumber" HeaderText="Customer Number" SortExpression="CustomerNumber" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:HyperLink ID="hlParent" runat="server" Text='<% #(Eval("Link")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
I modified the ASP from your original question by adding an ID and removing the reference to the NavigateUrl attribute from the HyperLink control.
Code
protected void grdLinks_RowDataBound( object sender, GridViewRowEventArgs e )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string link = DataBinder.Eval(e.Row.DataItem, "Link") as string;
if (link != null && link.Length > 0)
{
// "FindControl" borrowed directly from DOK
HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
if (hlParent != null)
{
// Do some manipulation of the link value
string newLink = "https://" + link
// Set the Url of the HyperLink
hlParent.NavigateUrl = newLink;
}
}
}
}
RowDataBound is called for every row in the GridView, including headers, footers, etc. Therefore, you should start by examining only the rows containing data.
Once you're on a row, there are several ways to examine the cells. One is just to use the cell index (2 here). That seems simple in your situation, but will lead to frustration if you ever rearrange the columns.
Here's an example of that from MSDN:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
A better way is to use FindControl with the item's ID.
protected void gvBarcode_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hlParent = (HyperLink)e.Row.FindControl("hlParent");
}
}
You may also want to look into letting the gridview do it for you.
You can use the datanavigateurlformatstring property to insert querystring parameters if that's what you are trying to do.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.datanavigateurlformatstring.aspx