How to fill data in some of GridViewRow and leave some blank - c#

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

Related

Insert Grid View column value into Textbox and dropdown on button control

I want to Fill gridview column value into given control
something like Project Title value fill inside project Title text box Problem ID fill inside Selected Problem Dropdown and so on... on button click even
i have taken as control name
Project title as txtProjectTitle,
selectProblem Id as ddlSelectProblem,
Project_Start_Date as txtProjectStartDate,
Project_Target_Date as TextBox1,
gridview as GrdTemp,
Procedd button as Button2_Click
ASPX CODE:
[![<asp:GridView ID="GrdTemp" runat="server" Style="width: 100%; text-align: center" class="table table-striped table-bordered" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="S.No." HeaderStyle-Width="5%">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("ID") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Title">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Title") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Problem ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Problem") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Start Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Start_Date") %>' ID="lblID</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Target Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Project_Target_Date") %>' ID="lblID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns> </asp:GridView>][2]][2]
C# code:
protected void Button2_Click(object sender, EventArgs e)
{
GridViewRow row = (sender as Label).NamingContainer as GridViewRow;
TextBox txtProject = row.FindControl("txtProjectTitle") as TextBox;
txtProject.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Title"]);
DropDownList ddlProblem = row.FindControl("ddlSelectProblem") as DropDownList;
ddlSelectProblem.SelectedItem.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Problem"]);
txtProjectStartDate.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Start_Date"]);
TextBox1.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["Project_Target_Date"]);
}

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>

Update column value while binding in gridview

I'd like to update the values of column Action based on the value of the column Status which is Boolean. If Status is True then value in Action must update to Deactivate. When I run, the Action field doesn't updates. I think there is an error in codebehind.
Here is the codebehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[4].Text;
TextBox TextBox2 = (TextBox)e.Row.FindControl("TextBox2");
if (value == "True")
{
TextBox2.Text = "Take";
}
else if (value == "False")
{
TextBox2.Text = "Available";
}
}
}
Here is the code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowSorting="True"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ShopNumber" HeaderText="ShopNumber" ItemStyle-Width="80" SortExpression="ShopNumber" >
</asp:BoundField>
<asp:BoundField DataField="ShopName" HeaderText="ShopName" ItemStyle-Width="80" SortExpression="ShopName" >
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="80"SortExpression="Address" >
</asp:BoundField>
<asp:BoundField DataField="Website" HeaderText="Website" ItemStyle-Width="80" SortExpression="Website" >
</asp:BoundField>
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Status") %>'> </asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" SortExpression="Action">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Action") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Action") %>'> </asp:TextBox>
</EditItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have a couple issues. First, you are trying to update the "Status" column by setting the text of TextBox2. In your question, you wanted to update the "Action" column.
Your second issue is that you are trying to update the TextBox in the EditItemTemplate of the column. On first run of the GridView, this template will not be shown. It will only be shown when the GridView is put into edit mode. What this means is that TextBox2 shouldn't even be found.
What you need to do is first use the the correct column, whichever that may be. If that truly is the "Action" column, first try setting the Label1 text value. If your GridView is in edit mode, set the TextBox1 text value instead.
You also have two EditItemTemplate in your "Action" column. You probably don't want that and that may be causing an error.

FindControl in gridview returns null

I am currently in a dilemma with my gridview not returning a label, which is within a detailsview...
My C# code is:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// get pet number for when removing a pet from reservation
int numberSelected = -1;
String numbertxt = "-1";
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.Rows[gv1.SelectedRow.RowIndex];
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
// find selected index, and get number in column 0
// label within GridView1 within dvReservation DetailsView
numbertxt = numberLbl.Text;
...
Gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="dsObjGet"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField InsertVisible="False" ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server"
Text='<%# Eval("NUMBER") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<AlternatingItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</AlternatingItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("NAME") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField SelectText="Remove" ShowSelectButton="True"
CausesValidation="False">
<ControlStyle CssClass="link" />
</asp:CommandField>
</Columns>
</asp:GridView>
When I breakpoint
Label numberLbl = (Label)rvRow.Cells[0].FindControl("lblNumber");
the label comes out as null (numberLbl)...
The message returned from the exception is:
"Object reference not set to an instance of an object"
EDIT:
This seems to be resolved if I generate lblNumber in an external gridview (on the page) with Eval("NUMBER"), though I don't see why it doesn't work in the current GridView I was trying to work with, given that GridView1 is within a DetailsView.
You should not use the Cell Collection when using FindControl. Just use this
GridView gv1 = (GridView)sender;
GridViewRow rvRow = gv1.SelectedRow;
Label numberLbl = (Label)rvRow.FindControl("lblNumber");

How to add Controlls on runtime to a Gridview?

First of all the ASPcode, the problemdescription below.
<asp:GridView ID="GridViewContacts" runat="server" ForeColor="#333333" DataKeyNames="L_ID_CONTACT" AllowPaging="True" AllowSorting="True"
OnPageIndexChanging="GridViewContacts_PageIndexChanging" PageSize="25" AutoGenerateColumns="False" OnRowCommand="GV_Contacts_RowCommand" >
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandArgument="Edit" CommandName="Edit" Text="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonView" runat="server" CommandArgument="View" CommandName="View" Text="View">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="L_Name" runat="server" Text='<%# Eval("L_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Companydetails">
<ItemTemplate>
<asp:Label ID="L_Companydetails" runat="server" Text='<%# Eval("L_Companydetails") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMail">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False" HeaderText="ID_CONTACT" >
<ItemTemplate>
<asp:Label Visible="false" ID="L_ID_CONTACT" runat="server" Text='<%# Eval("L_ID_CONTACT") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<%-- //Stylesettings here--%>
</asp:GridView>
Okay, then in the CodeBehind I have a Select from my database, where i select the ID_Contact, the Name, the Companydetails, which can be 1 per Row only.
On the RowCreated event I get the UserID of the actual User, and I select all E-Mails the user has, can be 0-10 per row.
Now my problem is:
How can i insert Linkbuttons with the onClick-event in the description into this part of my code?
Like this:
<asp:TemplateField HeaderText="EMail">
<ItemTemplate>
<asp:LinkButton[i] runat="server" onClick="SendEmail">
</asp:Linkbutton[i]>
<asp:LinkButton[i] runat="server" onClick="SendEmail">
</asp:Linkbutton[i]>
</ItemTemplate>
</asp:TemplateField>
So i want to add those controlls with code into THIS TemplateField.
Is this possible ?
Thoughts i allready had:
This.GridViewContacs.Controlls.AddAt(index,Linkbutton)
But also no clue here how it should work.
Thanks in advance,
me
Easiest is to add a placeholder control to the ItemTemplate, as ItemTemplate has no ID.
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="emails" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
and then in RowDataBound event
if (e.Row.RowType == DataControlRowType.DataRow)
{
PlaceHolder emails = e.Row.FindControl("emails") as PlaceHolder;
if (emails != null)
{
LinkButton lbEmail = new LinkButton();
lbEmail.Text = "your text";
lbEmail.Click += new EventHandler(SendEmail);
emails.Controls.Add(lbEmail);
}
}
Of course, the example is simplified. You can easily extend it to your needs.

Categories