Get cell values of textBoxes within ItemTemplate of a Gridview - c#

I have a GridView with textBoxes inside ItemTemplates. I can retrieve the data from the column that was binded to the Gridview on page load, however, I do not see any of the manually entered text when reading the cell values.
The HTML:
<asp:GridView ID="tblRentDue" runat="server" ClientIDMode="Static">
<Columns>
<asp:BoundField DataField="Application_ID" HeaderText="Application ID" HeaderStyle-Wrap="false" />
<asp:BoundField DataField="Month" HeaderText="Month" HeaderStyle-Wrap="false" />
<asp:TemplateField HeaderText="Orignal Amount Due">
<ItemTemplate>
<asp:TextBox ID="txtOrignalAmountDue" runat="server" TextMode="Number" Text='<%# Eval("Original_Amount_Due") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fees Due">
<ItemTemplate>
<asp:TextBox ID="txtFeesDue" runat="server" TextMode="Number" Text='<%# Eval("Fees_Due") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount Paid">
<ItemTemplate>
<asp:TextBox ID="txtAmountPaid" runat="server" TextMode="Number" Text='<%# Eval("Amount_Paid") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Still Due">
<ItemTemplate>
<asp:TextBox ID="txtTotalStillDue" runat="server" TextMode="Number" Text='<%# Eval("Total_Still_Due") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The code behind:
public DataTable GetDataTable(GridView dtg)
{
DataTable dt = new DataTable();
dt.Columns.Add("Month");
dt.Columns.Add("Original_Amount_Due");
dt.Columns.Add("Fees_Due");
dt.Columns.Add("Amount_Paid");
dt.Columns.Add("Total_Still_Due");
foreach (GridViewRow row in dtg.Rows)
{
DataRow dr;
dr = dt.NewRow();
dr["Month"] = (row.Cells[0].Text); //These values are showing up and was loaded from the database.
dr["Original_Amount_Due"] = (row.Cells[1].Text); //Nothing showing from user input
dr["Fees_Due"] = (row.Cells[2].Text); //Nothing showing from user input
dr["Amount_Paid"] = (row.Cells[3].Text); //Nothing showing from user input
dr["Total_Still_Due"] = (row.Cells[4].Text); //Nothing showing from user input
dt.Rows.Add(dr);
}
return dt;
}

The solution I found - within the foreach loop for each textbox:
TextBox orignalAmount = (TextBox)row.Cells[1].FindControl("txtOrignalAmountDue");
dr["Original_Amount_Due"] = orignalAmount.Text;
There may be a less clunky way of doing this but it does work.

Related

add chosen plugin to dropdown list that is dynamically add within a dynamic gridview ASP.net

I have a dynamic grid view with different controls that added dynamically to this grid ,and one of them is drop-down list control and the data source for this drop-down list is sql table ,
My request is: I need to add a chosen plugin to ddl control that is created dynamically whit in each grid view row created ,I can easily deal with another gridview footer with below code:
$('#<%=gv_employee_Entity.FooterRow.Cells[6].FindControl("ddl_full_Crs_name").ClientID%>').chosen();
but I can't do that with a dynamic created data row
button and textbox to add gridview rows:
<asp:TextBox ID="txt_no_of_records" runat="server"></asp:TextBox>
&nbsp
<asp:Button ID="btn_add_rows" runat="server" Text="Add Rows" OnClick="btn_add_rows_Click" />
my grid view:
<asp:GridView ID="gv_employees" runat="server" AutoGenerateColumns="false" CellPadding="5">
<Columns>
<asp:TemplateField HeaderText="SL NO.">
<ItemTemplate>
<asp:TextBox ID="txt_emp_id" runat="server" Text="<%#Container.DataItemIndex + 1 %>"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="employee name">
<ItemTemplate>
<asp:TextBox ID="txt_emp_name" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<asp:TextBox ID="txt_emp_job_title" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Gender">
<ItemTemplate>
<asp:TextBox ID="txt_emp_gender" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:TextBox ID="txt_note" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Directorate Name">
<ItemTemplate>
<asp:DropDownList ID="ddl_direct_name" AppendDataBoundItems="true" runat="server" DataSourceID="lds_direct_name" DataTextField="Direct_name" DataValueField="Direct_name">
<asp:ListItem Text="select value"></asp:ListItem>
</asp:DropDownList>
<asp:LinqDataSource runat="server" EntityTypeName="" ID="lds_direct_name" ContextTypeName="TrainingManagementSystem.Models.emp_trainingDataContext" Select="new (Direct_name)" TableName="tbl_Directorates"></asp:LinqDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind button click event to add rows:
protected void btn_add_rows_Click(object sender, EventArgs e)
{
AddRowsToGrid();
}
protected void AddRowsToGrid()
{
List<int> noofrows = new List<int>();
int rows = 0;
int.TryParse(txt_no_of_records.Text.Trim(), out rows);
for (int i = 0; i < rows; i++)
{
noofrows.Add(i);
}
gv_employees.DataSource = noofrows;
gv_employees.DataBind();
if (gv_employees.Rows.Count > 0)
Panel1.Visible = true;
else
Panel1.Visible = false;
}

How to fill data in some of GridViewRow and leave some blank

ASP.net C#
I am creating a gridview having 5 rows containing detail of operations users have to perform. Detail of operations should be filled when it is completed. At a time any number of operation details can be filled.
In gridview, first Item-template contains Label (for operation name) and others are textbox (for other details).
If any user has filled 3 rows then rest of two rows should be blank.
My problem is how to bind those 3 rows filled previously leaving two bottom rows available for entry.
My Gridview design is :
<asp:GridView CssClass="table-bordered gridStyle" runat="server" ShowFooter="True"
ID="grdOperationEntry" GridLines="None" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Operation">
<ItemTemplate>
<asp:Label Text='<%# Eval("operation_title") %>' ID="lblOperationName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Completion">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("date_completed") %>' CssClass="form-control"
ID="txtDateCompletion" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time Taken">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("time_taken") %>' ID="txtTimeTaken" CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Score">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("score_gain") %>' ID="txtScore" CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reported To">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("reported_to") %>' ID="txtReportedTo"
CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>
Add a Label with all TextBoxes in GridView and set them visible false and bind also Label from database. I have added an example TemplateField to below, you can do it for all other:
.....
<asp:TemplateField HeaderText="Date of Completion">
<ItemTemplate>
<asp:Label runat="server" Visible="False" Text='<%# Eval("date_completed")
ID="lblDateCompletion" %>'></asp:Label>
<asp:TextBox runat="server" Visible="False" Text='<%# Eval("date_completed") %>'
ID="txtDateCompletion" />
</ItemTemplate>
</asp:TemplateField>
.....
In RowDataBound event set them visible true:
protected void grdOperationEntry_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if gridview row not a header or footer
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get controls by id from gridview and cast them
Label lblDateCompletion = e.Row.FindControl("lblDateCompletion") as Label;
TextBox txtDateCompletion = e.Row.FindControl("txtDateCompletion") as TextBox;
if (lblDateCompletion.Text == null)
txtDateCompletion.Visible = true;
else
lblDateCompletion.Visible = true;
// perform same for other controls
}
}
Note: Don't forget to add OnRowDataBound property to your GridView <asp:GridView ID="grdOperationEntry" runat="server" OnRowDataBound="grdOperationEntry_RowDataBound" >

How can I get SelectedIndexChanged to get companyname instead of companyid into textbox, 2 different tables?

I have 2 tables that I want to get data from. The first table is the Contractor table which has the contractors personal information and the second table is the Company table which has 2 columns, CompanyName and CompanyID. Both tables have identical column, CompanyID.
The current code I have has a gridview with a "Select" column, SelectedIndexChanged returns the values from the database to fill textboxes.
What I want to do is get the name of the company as opposed to the companyID filled into the textbox (txtCompany), how can I modify my current code below to do so?
ASPX
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPID" runat="server"></asp:TextBox>
<asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
<asp:GridView ID="TDetails" DataSourceID="SqlDataSource1" DataKeyNames="PersonID" AutoGenerateColumns="false" runat="server" OnSelectedIndexChanged="TDetails_SelectedIndexChanged" >
<HeaderStyle BackColor="gray" ForeColor="White" />
<Columns
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnSelect" runat="server" CommandName="Select" Text="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company" Visible="false">
<ItemTemplate>
<asp:Label ID="lblCompany" runat="server" Text='<%#Eval("CompanyID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PersonID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblPID" runat="server" Text='<%#Eval("PersonID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.CS
protected void TDetails_SelectedIndexChanged(object sender, EventArgs e)
{
txtPID.Text = TDetails.DataKeys[TwicDetails.SelectedRow.RowIndex].Value.ToString();
txtLastName.Text = (TDetails.SelectedRow.FindControl("lblLastName") as Label).Text;
txtFirstName.Text = (TDetails.SelectedRow.FindControl("lblFirstName") as Label).Text;
txtCompany.Text = (TDetails.SelectedRow.FindControl("lblCompany") as Label).Text;
}
You can modify your SQL query to fetch the CompanyName as well like how you are fetching the CompanyId and bind it to gridview and set the visibility to false, finally you can set the value of textbox like how you are doing for other controls:-
SQL Query:-
SELECT CompanyId, CompanyName, ....
FROM tblCompany..
Aspx:-
<asp:TemplateField HeaderText="Company" Visible="false">
<ItemTemplate>
<asp:Label ID="lblCompanyName" runat="server" Text='<%#Eval("CompanyName") %>' />
</ItemTemplate>
</asp:TemplateField>

How to put each cell value for each row from GridView in variables

I tried some ways in stackoverflow solutions. But Those are not implemented well by me. Most of the time I got null/empty for the variables and Exception like "Object reference not set to an instance of an object."
My GridView:
<asp:GridView runat="server" ID="TestReportGridView" AutoGenerateColumns="false" Width="370px">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<%#Eval("TestId") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sr">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<%#Eval("TestName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fee">
<ItemTemplate>
<%#Eval("Fee") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>
I want to put cell text in 4 different variables for each row when I click in Save button:
protected void SaveButton_Click(object sender, EventArgs e)
{
string patient = PatientNameTextBox.Text;
string birthDate = BirthDateTextBox.Text;
string mobile = MobileNoTextBox.Text;
int rows = TestReportGridView.Rows.Count;
foreach (GridViewRow row in TestReportGridView.Rows)
{
Label test = (Label)row.FindControl("Test");
string testName = test.Text;
//Label lblQuantity = (Label)row.FindControl("Quantity");
//string Quantity = lblQuantity.Text;
}
}
Image of the UI
I added a label in ItemTemplate. does it right way to add Label
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="Test" runat="server" ><%#Eval("TestName") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
You need to add Label control in your ItemTemplate. An example below:
<ItemTemplate>
<asp:Label ID="Test" runat="server" Text='<%# Eval("TestName") %>' />
</ItemTemplate>
Adding <%#Eval("TestId") %> directly inside ItemTemplate will not automatically add any control to search from code behind.

Binding a DataTable to Template Fields of a GridView

I can successfully bind a DataTable to a GridView by auto generating columns, but I need to display a multi-lined cell for one of the columns. To do this, I want to use a template field with an item template using a TextBox object. I fill in the DataTable by adding columns and then adding the rows. I know my datatable is set up right because it shows all the data (except for the multi-lined cell) as I want it. My issue is getting the gridview to extract my data based on the column names and filling in the template fields I have set up. If I turn AutoGenerateColumns off then the 4 templatefield columns still appear (in the correct amount according to the datatable too), just blank, and if I have it set to true, then the 4 blank columns appear as well as 4 additional columns with the same headers that contain my data using the defaults for what the cell contains to display the information.
<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="False" HeaderStyle-BorderStyle="None" CellPadding="3" ItemStyle-Wrap="true" >
<Columns>
<asp:TemplateField HeaderText="Code">
<asp:ItemTemplate>
<asp:Label ID="txtCode" runat="server" Text='<%# Eval("Code") %>'>
</asp:Label>
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<asp:ItemTemplate>
<asp:Label ID="txtTitle" runat="server" Text='<%# Eval("Title") %>'>
</asp:Label>
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class">
<asp:ItemTemplate>
<asp:Label ID="txtClass" runat="server" Text='<%# Eval("Class") %>'>
</asp:Label>
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="History">
<asp:ItemTemplate>
<asp:TextBox ID="txtHistory" runat="server" IsReadOnly="true" Text='<%# Eval("History")%>'>
</asp:TextBox>
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The above is the portion of my asp.net code that relates to the GridView in question. Following is how i set up my DataTable and bind it.
DataTable table = new DataTable();
table.Columns.Add("Code", typeof(string));
table.Columns.Add("Title", typeof(string));
table.Columns.Add("Class", typeof(string));
table.Columns.Add("History", typeof(string));
for (int i = 0; i < index; i++)
{
table.Rows.Add(docs[i].Code, docs[i].Name, docs[i].Class.Name, history[i]);
}
DataGrid1.DataSource = table;
DataGrid1.DataBind();
Change .aspx code like below
<Columns>
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:Label ID="txtCode" runat="server" Text='<%# Eval("Code") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="txtTitle" runat="server" Text='<%# Eval("Title") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class">
<ItemTemplate>
<asp:Label ID="txtClass" runat="server" Text='<%# Eval("Class") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="History">
<ItemTemplate>
<asp:TextBox ID="txtHistory" runat="server" IsReadOnly="true"
Text='<%# Eval("History")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
foreach (GridViewRow grdRow in grdMenuPermitted.Rows)
{
DataRow drow = dt.NewRow();
Label lblMenuName = (Label)grdRow.FindControl("lblMenuName");
HiddenField hdnID = (HiddenField)grdRow.FindControl("hdnID");
drow["MenuName"] = lblMenuName.Text;//grdRow.Cells[0].Text;
drow["MenuID"] = hdnID.Value;
//drow["lname"] = grdRow.Cells[3].Text;
dt.Rows.Add(drow);
}

Categories