GridViewCosts.SelectedRow.Cells[1].Text dont work with itemTemplate - asp.net

I have this gridView:
<div class ="gridView">
<asp:GridView ID="GridViewCosts" runat="server" ShowFooter="True" ShowHeaderWhenEmpty="True"
AutoGenerateColumns="False" OnRowDeleting="GridViewCosts_RowDeleting" Width="387px"
OnSelectedIndexChanged="GridViewCosts_SelectedIndexChanged"
OnPageIndexChanging="GridViewCosts_PageIndexChanging"
AllowPaging="True"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" PageSize="5">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Номер" />
<asp:TemplateField HeaderText="Стойност">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Value") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
</asp:TemplateField>
And in code behind i Want :
protected void GridViewCosts_SelectedIndexChanged(object sender, EventArgs e)
{
TextBoxValue.Text = GridViewCosts.SelectedRow.Cells[1].Text;
}
this is not working when the column Value is TemplateField. If the column is BoundFIeld it's working . What should i do ?

If you use TemplateFields you have to use FindControl to get a reference to the control:
Label Label2 = (Label)GridViewCosts.SelectedRow.FindControl("Label2");
TextBoxValue.Text = Label2.Text;

Try this sample code
For Each gvr As GridViewRow In gvInvoice.Rows
Dim TctValue As TextBox= DirectCast(GridViewCosts.Cells(1).FindControl("TextBox2"), TextBox)
totamount = totamount + Convert.ToDouble(lblAmount.Text)
Next
txtTotal.Text = totamount.ToString()

Related

How to show and hide textbox in gridview on link button click

I have gridview,which has approve and reject button.When click on reject button i need to show textbox for comments else no.after enetering comments i have save it in data base.How to do this can any one help me
here is my code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Industrial Zone">
<ItemTemplate>
<asp:Label runat="server" ID="lblindzone" Text='<%# Eval("indzone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="District">
<ItemTemplate>
<asp:Label runat="server" ID="lbldstr" Text='<%# Eval("dstr")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Industrial Area">
<ItemTemplate>
<asp:Label runat="server" ID="lblnmindar" Text='<%# Eval("nmindar")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plot Number">
<ItemTemplate>
<asp:Label runat="server" ID="lblplno" Text='<%# Eval("plno")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve/Reject">
<ItemTemplate>
<asp:LinkButton ID="lnkApproved" runat="server" Text="Approve" OnClick="lnkApproved_Click"><img src="images/approve.png" style="width:20px;height:20px; margin:5px" title="Approve"/></asp:LinkButton>
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
<asp:LinkButton ID="lnkviewdetails" runat="server" Text="View Details" OnClick="lnkviewdetails_Click"><img src="images/viewdetails.png" style="width:20px;height:20px; margin:5px" title="Details" /></asp:LinkButton>
<asp:TextBox ID="txtcomment" runat="server" style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="lblstatus" Text='<%# Eval("status")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am assuming you are doing it with only C# and no Javascript. to view or hide a specific textbox you should trigger a RowCommand Event of GridView:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "viewhide")
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tbComments = ((TextBox)gvr.FindControl("txtcomment"));
tbComments.Visible = true;
}
}
And this would be your ASPX Codebehind
<asp:LinkButton ID="lnkReject" runat="server" Text="Reject" OnClick="lnkReject_Click" CommandName="viewhide" OnClientClick="return showandhide(this)"><img src="images/reject.png" style="width:16px;height:16px; margin:5px" title="Reject"/></asp:LinkButton>
EDIT: Now You've changed the tag from C# to VB.NET which is not good. because i consumed all my time writing a c# code!

Get values from GridView and place it in text boxes

I just want to get the cell values from my GridView and place it in the different textboxes. I tried so many things but I just couldn't get it to work. Maybe you could help. Thanks.
This is the last thing I tried:
<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" CellSpacing="2" AutoGenerateColumns="False" AutoGenerateSelectButton="True" onselectedindexchanged="GridView1_SelectedIndexChanged" OnSelectedIndexChanging="Gridview1_SelectedIndexChanging">
<Columns>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%# Eval("emp_id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="30px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("emp_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("emp_add") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact Num.">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("emp_contact") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hire Date">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("hire_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch ">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("hire_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
Code Behind:
protected void btnUpdate_Click(object sender, EventArgs e)
{
EmployeeDLL edll = new EmployeeDLL();
edll.Update_Employee(txtEmpID.Text, txtName.Text, txtAddress.Text, txtContact.Text, txtHireDate.Text, txtBranchID.Text);
lblStatus.Text = "Record has been updated.";
edll = null;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// txtEmpID.Text = GridView1.SelectedValue.ToString();
GridViewRow row = GridView1.SelectedRow;
txtEmpID.Text = row.Cells[0].Text;
txtName.Text = row.Cells[1].Text;
}
Use something like this
TextBox TextBoxLot = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("label");
When using <asp:TemplateField>, the data is not inside Cell but is contained in the Control present inside the Cell.
So, There are two steps to read the value:
Access the Control inside Cell
Access the Value from Control in step 1
Example:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
// Step 1: Access the Control inside Cell
Label lblEmpId = (Label)row.Cells[0].FindControl("lblEmpID");
// Step 2: Access the Value from Control in step 1
string value = lblEmpId.Text;
// Combine Step 1 & Step 2
string valueEmpID = ((Label)row.Cells[0].FindControl("lblEmpID")).Text;
string name = ((Label)row.Cells[0].FindControl("Label2")).Text;
}

ASP.NET DataGrid Select ItemTemplate By Condition

This is My DataGrid:
<asp:DataGrid ID="dgAll" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtB" runat="server" Text='<%#Eval("Value")%>' ></asp:TextBox>
</ItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="drdL" runat="server" DataSource='<%#GetComboData(Eval("Value"))%>'></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
As you see I have a TextBox and a DropDownList, But I need just one of them in each row, So the question is how can I select ItemTemplate by condition, assume if Eval("Type") == "TextBox") I need TextBox and if Eval("Type") == "DropDown") I need DropDown in that Row. Does any one have any idea about this?
You can use both the controls in the same template field and use code as below
<asp:DataGrid ID="dgAll" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtB" runat="server" Text='<%#Eval("Value")%>'
Visible ='<%# Eval("Type").ToString()=="TextBox"%'>
</asp:TextBox>
<asp:DropDownList ID="drdL" runat="server"
DataSource='<%#GetComboData(Eval("Value"))%>'
Visible ='<%# Eval("Type").ToString()!="TextBox"%'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
why do you use couple asp:TemplateColumn, you can use too like that,
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="txtB" runat="server" Text='<%#Eval("Value")%>' Visible="false" ></asp:TextBox>
<asp:DropDownList ID="drdL" runat="server" Visible="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
and in the Grid's ItemDataBound event
protected void dgAll_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemIndex == -1) return;
if (e.Item.ItemIndex % 2 == 0)
e.Item.FindControl("drdL").Visible = true;
else
e.Item.FindControl("txtB").Visible = true;
}

Row Edit event handler issue

<asp:GridView ID="GridView1" runat="server"
style="height: 121px; width: 175px" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
onrowediting="GridView1_RowEditing" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ButtonType="Button" SelectText="Edit" ShowEditButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCpuName" runat="server" Text='<%# Eval("cpuname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("cpuname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("status") %>'>></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
GridViewRow row = GridView1.Rows[e.NewEditIndex];
TextBox txt = (TextBox)row.FindControl("TextBox2");
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = txt.Text;
SqlDataSource1.UpdateParameters["cpuid"].DefaultValue = "3";
SqlDataSource1.Update();
BindGridView();
}
I am getting a object reference not set to an object on the textbox txt. I am not able to get the textbox2 in my gridview.
Can you try this
GridViewRow row = GridView1.Rows[GridView1.NewEditIndex];
TextBox txt = row.Cells[1].Controls[1] as TextBox;
If (txt!= null)
{
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = txt.Text;
SqlDataSource1.UpdateParameters["cpuid"].DefaultValue = "3";
SqlDataSource1.Update();
BindGridView();
}
Why you just don't get directly the textbox value? is this a great problem?
...
GridViewRow row = GridView1.Rows[e.NewEditIndex];
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = row.Columns["CpuName"].Value;
...
I don't know that this works, because I'm a C#-er, and basically work on winform not on web apps
First, here is the snippet from MSDN, RowEditing topic:
The RowEditing event is raised when a
row's Edit button is clicked, but
before the GridView control enters
edit mode. This enables you to provide
an event-handling method that performs
a custom routine, such as canceling
the edit operation, whenever this
event occurs.
To resolve the problem, handle the RowUpdating event for this purpose. There is an example which should be helpful to you at:
GridView.RowEditing event
Also, I believe you should use the Bind word to bind your TextBox to a field:
'>
For more details, please refer to the Data-Binding Expressions Overview

gridview control problem

work on C# asp.net vs05. i take a combo on gridview template field.
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True" SortExpression="StudentID" />
<asp:TemplateField HeaderText="DivisionName" SortExpression="DivisionName">
<ItemTemplate>
<asp:Label ID="lblDivisionName" runat="server" Text='<%# Bind("DivisionName") %>'
Width="116px"></asp:Label><br />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentName" SortExpression="StudentName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("StudentName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="Update" />
</Columns>
</asp:GridView>
Now i want to fill this combo by the bellow code ?
DropDownList1.DisplayMember = "CommercialRegionName";
foreach (object oItem in collection)
{
DropDownList1.Items.Add(oItem);
}
how to get the combo control id from the grid
You can use the following code:
Set OnRowDataBound of the gridview in aspx as OnRowDataBound="GridView3_RowDataBound"
Then put the following code in aspx.cs page.
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList1 =(DropDownList) e.Row.FindControl("DropDownList1");
DropDownList1.DisplayMember = "CommercialRegionName";
foreach (object oItem in collection)
{
DropDownList1.Items.Add(oItem);
}
}
}

Resources