Gridview controllers null after inserting column - c#

I am developing a Web App for managing Student exam entries but I am coming across an issue with my gridview.
The basic idea is that a user can go on and enter and update details on a students exam. Complications arise when certain subjects require different fields to be populated and this is where my issue is.
The exception is thrown when the Save Button method is run. I get a Null Object reference thrown when running the AddAudit and UpdateRecord methods. After some debugging from the looks of it the issue is the controls (ddlDate, txtAssesmentLevel etc) are not being declared from the FindCotrol method of the gridview meaning when the AddAudit and UpdateRecord methods are called they point to a null controller.
Be aware that this issue does not occur when the code is not "C2555" which leads me to suspect the issue is with dynamically adding columns and controls getting muddled up though i am not sure.
Any assistance would be great and feel free to ask for more information.
Below is my basic code:
Page Load Method
protected void Page_Load(object sender, EventArgs e){
if(!IsPostBack){
//Selected class passed through
selectedClass sc = (selectedClass)Session["selectedClass"] as selectedClass;
//Get Class Code
lblAosCode.Text = sc.getAOSCode();
//If class is English
if(lblAosCode.Text == "C2555"){
TemplateField speakingListening = new TemplateField();
speakingListening.HeaderText = "Speaking and Listening";
dgvSelectedClasses.Columns.Insert(7, speakingListening);
}
//Populate Gridview
DataTable dsSelectedClasses = AccessData.getSelectedClasses(sc.getAOSCode(), sc.getAOSPeriod(), sc.getDescription());
dgvSelectedClasses.DataSource = dsSelectedClasses;
dgvSelectedClasses.DataBind();
//Check if txtAssesmentLevel is populated
for (int index = 0; index < dgvSelectedClasses.Rows.Count; index++)
{
TextBox txtAssessmentLevel = (TextBox)dgvSelectedClasses.Rows[index].FindControl("txtAssessmentLevel");
if (dgvBefore.Rows[index].Cells[4].Text != " ")
{
txtAssessmentLevel.ReadOnly = true;
}
}
}
}
Save Method (Exception Thrown)
protected void btnSave_Click(object sender, EventArgs e)
{
for (int i = 0; i < dgvSelectedClasses.Rows.Count; i++)
{
DropDownList ddlL1L2 = (DropDownList)dgvSelectedClasses.Rows[i].FindControl("ddlL1L2");
DropDownList ddlExamDate = (DropDownList)dgvSelectedClasses.Rows[i].FindControl("ddlExamDate");
TextBox txtAssessmentLevel = (TextBox)dgvSelectedClasses.Rows[i].FindControl("txtAssessmentLevel");
DropDownList ddlSpeakingListening = null;
if (lblAosCode.Text.Contains("C2555"))
{
ddlSpeakingListening = (DropDownList)dgvSelectedClasses.Rows[i].FindControl("ddlSpeakingListening");
}
if (IsPostBack)
{
if (lblAosCode.Text.Contains("C2555"))
{
AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Assessment Level", txtAssessmentLevel.Text, dgvBefore.Rows[i].Cells[4].Text);
AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam request L1 or L2", ddlL1L2.SelectedValue, dgvBefore.Rows[i].Cells[14].Text);
AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam Date", ddlExamDate.SelectedValue, dgvBefore.Rows[i].Cells[15].Text);
AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Speaking and Listening", ddlSpeakingListening.SelectedValue, dgvBefore.Rows[i].Cells[7].Text);
AccessData.updateRecord(txtAssessmentLevel.Text, ddlL1L2.SelectedValue, ddlExamDate.SelectedValue, lblAosCode.Text, lblAosPeriod.Text, dgvSelectedClasses.Rows[i].Cells[0].Text, ddlSpeakingListening.SelectedValue);
}
else
{
AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Assessment Level", txtAssessmentLevel.Text, dgvBefore.Rows[i].Cells[4].Text);
AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam request L1 or L2", ddlL1L2.SelectedValue, dgvBefore.Rows[i].Cells[13].Text);
AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam Date", ddlExamDate.SelectedValue, dgvBefore.Rows[i].Cells[14].Text);
AccessData.updateRecord(txtAssessmentLevel.Text, ddlL1L2.SelectedValue, ddlExamDate.SelectedValue, lblAosCode.Text, lblAosPeriod.Text, dgvSelectedClasses.Rows[i].Cells[0].Text);
}
}
}
Response.Redirect("~/contact");
}
On Row Data Bound
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (lblAosCode.Text.Contains("C2555"))
{
DropDownList ddlSpeakingListening = new DropDownList();
ddlSpeakingListening.ID = "ddlSpeakingListening";
ddlSpeakingListening.Items.Add("L1");
ddlSpeakingListening.Items.Add("L2");
ddlSpeakingListening.Items.Add("Entry");
ddlSpeakingListening.Items.Add("NS");
e.Row.Cells[7].Controls.Add(ddlSpeakingListening);
}
}
}
ASP.NET
<asp:GridView ID="dgvSelectedClasses" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="Student ID" ReadOnly="True" />
<asp:BoundField DataField="StageCode" HeaderText="Stage Code" ReadOnly="True" />
<asp:BoundField DataField="Forename" HeaderText="Forename" ReadOnly="True" />
<asp:BoundField HeaderText="Surname" DataField="Surname" />
<asp:TemplateField HeaderText="Assessment Level">
<ItemTemplate>
<asp:TextBox ID="txtAssessmentLevel" Text ='<%#Bind("AssessmetLevel") %>' runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TargetLevel" HeaderText="Target Level" />
<asp:BoundField DataField="Achievelevel" HeaderText="Achieve Level" />
<asp:BoundField DataField="FELSOutcome" HeaderText="FELS Outcome" />
<asp:BoundField DataField="Registration" HeaderText="Registration" />
<asp:BoundField DataField="DateSpreadsheetSent" HeaderText="Last Update" />
<asp:BoundField DataField="Dayofclass" HeaderText="Day of class" />
<asp:BoundField DataField="Timeofclass" HeaderText="Time of class" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:TemplateField HeaderText="Exam request L1 or L2" >
<ItemTemplate>
<asp:DropDownList ID="ddlL1L2" runat="server" >
<asp:ListItem>Not Set</asp:ListItem>
<asp:ListItem>L1</asp:ListItem>
<asp:ListItem>L2</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam date">
<ItemTemplate>
<asp:DropDownList ID="ddlExamDate" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="dgvBefore" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="Student ID" ReadOnly="True" />
<asp:BoundField DataField="StageCode" HeaderText="Stage Code" ReadOnly="True" />
<asp:BoundField DataField="Forename" HeaderText="Forename" ReadOnly="True" />
<asp:BoundField HeaderText="Surname" DataField="Surname" />
<asp:BoundField DataField="AssessmetLevel" HeaderText="Assessment Level" NullDisplayText=" "/>
<asp:BoundField DataField="TargetLevel" HeaderText="Target Level" />
<asp:BoundField DataField="AchieveLevel" HeaderText="Achieve Level" />
<asp:BoundField DataField="FELSOutcome" HeaderText="FELS Outcome" />
<asp:BoundField DataField="Registration" HeaderText="Registration" />
<asp:BoundField DataField="DateSpreadsheetSent" HeaderText="Date Spreadsheet Sent" />
<asp:BoundField DataField="Dayofclass" HeaderText="Day of class" />
<asp:BoundField DataField="Timeofclass" HeaderText="Time of class" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="ExamrequestL1orL2" HeaderText="Exam request L1 or L2" />
<asp:BoundField DataField="Examdate" HeaderText="Exam date" />
<asp:BoundField DataField="Reviewed" HeaderText="Reviewed" />
</Columns>
</asp:GridView>
P.S. This is my first question hopefully it makes sense and I am open to pointers :)
Edit: The page load method does include code to populate dgvBefore as well as a few authentications thing i just forgot to include it.

I think you need to add the drop down list column on every postback. Refer this link : https://www.codeproject.com/Tips/682689/Add-populated-dropdownlist-to-GridView-dynamically

Related

PageIndex changes but data stays the same

I have a GridView that I bind a list to from data retrieved via edmx. My issue is when I click on pagers, the page changes, and I have debugged and know that it's changing the value, but it always just shows the data from the first page. What am I missing? Oh, and I call LoadAllRequestsData() from Page_Load in an if(!Page.IsPostBack).
<cm:GridControl runat="server" ID="gvAllRequests" DataKeyNames="Number" OnPageIndexChanging="AllRequestsGridViewPageIndexChanging" ShowHeaderWhenEmpty="True" EmptyDataText="No requests to show." >
<Columns>
<asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" InsertVisible="False" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" SortExpression="CustomerId" Visible="False"></asp:BoundField>
<asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer" Visible="False"></asp:BoundField>
<asp:BoundField DataField="TypeId" HeaderText="TypeId" SortExpression="TypeId" Visible="False"/>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type"></asp:BoundField>
<asp:BoundField DataField="Note" HeaderText="Note" SortExpression="Note"/>
<asp:BoundField DataField="RequestedOn" HeaderText="Requested On" SortExpression="RequestedOn"/>
<asp:BoundField DataField="RequestedById" HeaderText="RequestedById" SortExpression="RequestedById" Visible="False" />
<asp:BoundField DataField="RequestedBy" HeaderText="Requested By" SortExpression="RequestedBy" />
<asp:BoundField DataField="CompletedOn" HeaderText="Completed On" SortExpression="CompletedOn" />
<asp:BoundField DataField="CompletedById" HeaderText="CompletedById" SortExpression="CompletedById" Visible="False"/>
<asp:BoundField DataField="CompletedBy" HeaderText="Completed By" SortExpression="CompletedBy" />
<asp:BoundField DataField="LastModifiedOn" HeaderText="LastModified On" SortExpression="LastModifiedOn" />
<asp:BoundField DataField="LastModifiedById" HeaderText="LastModifiedById" SortExpression="LastModifiedById" Visible="False"/>
<asp:BoundField DataField="LastModifiedBy" HeaderText="LastModified By" SortExpression="LastModifiedBy" />
<asp:CheckBoxField DataField="IsDeleted" HeaderText="IsDeleted" SortExpression="IsDeleted" Visible="False"/>
</Columns>
</cm:GridControl>
private void LoadAllRequestsData(string sortExpression = "RequestedOn", SortDirection sortDirection = SortDirection.Descending)
{
var db = new CrewManagerEntities();
var list = db.GetAllRequestsByUserId(_customerId).ToList();
if (!string.IsNullOrEmpty(sortExpression))
{
list = list.AsQueryable().OrderBy(sortExpression + " " + (sortDirection == SortDirection.Ascending ? "ASC" : "DESC")).ToList();
gvAllRequests.SetSort(sortExpression, sortDirection);
}
gvAllRequests.DataSource = list;
gvAllRequests.DataBind();
}
protected void AllRequestsGridViewPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvAllRequests.PageIndex = e.NewPageIndex;
LoadAllRequestsData();
}
Edit: So, I tried using SqlDataSource instead of doing call in codebehind and using a list and the paging works. That is fine but I would like to know why the list doesn't work as there may be instances where I need to use a list source. Any ideas?

DropDownList control returning null within GridView during casting

I am very new to development in asp.net.
What I am TRYING to do is upon edit of a gridrow, provide a drop down list for a particular column.
User story: User enters text into termSearch textbox; A list of items matching the search criteria is returned. The Activity Status column has only 2 valid values; active, inactive.
My problems are as follows:
DropDownList/Text not reflected in designer if it is within TemplateField. When outside of TemplateField, designer detects it.
During a cast, value is being returned as null.
During DataSource method, drop down list (ddlActivity) returning null.
GridView (Activity Status Template Field)
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField DataField="CODE" ReadOnly="True" HeaderText="Term Code" HtmlEncode="False" Visible="true">
<ItemStyle Width="24%" />
</asp:BoundField>
<asp:BoundField DataField="DISPLAYLABEL" HeaderText="Label" HtmlEncode="False" Visible="true">
<ItemStyle Width="24%" />
</asp:BoundField>
<asp:TemplateField HeaderText="Activity Status">
<ItemTemplate>
<asp:TextBox ID="lblActivity" runat="server" Text='<%#Bind("STATUS_FK") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlActivity" runat="server" SelectedValue='<%# Bind("STATUS_FK") %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="STRINGATTRIBUTE" HeaderText="String Attribute" HtmlEncode="False" Visible="true">
<ItemStyle Width="24%" />
</asp:BoundField>
<asp:BoundField DataField="LONGLABEL" HeaderText="Long Label" HtmlEncode="False" Visible="true">
<ItemStyle Width="24%" />
</asp:BoundField>
</Columns>
Code Behind
protected void gvSearch_DataBound(object sender, GridViewRowEventArgs e)
{
string code = termSearch.Text;
ddlActivity.DataSource = termDAO.SearchByCode(code);
DropDownList ddlActivityStatus = (DropDownList)e.Row.FindControl("ddlActivity");
ddlActivityStatus.Items.Insert(0, new ListItem("--Select a Status--", "0"));
ddlActivityStatus.Items.Add(new ListItem("Active", "STATUS.A"));
ddlActivityStatus.Items.Add(new ListItem("Inactive", "STATUS.I"));
}
ddlActivity is in the EditItemTemplate. So you have to check for that.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlActivityStatus = (DropDownList)e.Row.FindControl("ddlActivity");
}
}
}

Select complete rows from a gridview using a checkbox and transfer to another webform

I need help to select information from a gridview not to delete, I´ve seen multiple ways to delete and I've tried to adapt it to my code but I'm having some issues with it,
This is my gridview code
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ItemStyle-Width="150px" ID="cbSelect" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelect_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ItemStyle-Width="150px" ID="cbSelected" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelected_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Poliza">
<ItemTemplate>
<asp:Label ID="Poliza" runat="server" Text='<%# Bind("POLIZA") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="RAMO"
HeaderText="Ramo" />
<asp:BoundField ItemStyle-Width="150px" DataField="CERTIF"
HeaderText="Certificado" />
<asp:BoundField ItemStyle-Width="150px" DataField="NOMRMO"
HeaderText="Ramo" />
<asp:BoundField ItemStyle-Width="150px" DataField="CIA"
HeaderText="N. Comp" />
<asp:BoundField ItemStyle-Width="150px" DataField="NOMCIA"
HeaderText="Compañia" />
<asp:BoundField ItemStyle-Width="150px" DataField="RIF"
HeaderText="Ident" />
<asp:BoundField ItemStyle-Width="150px" DataField="CCT"
HeaderText="Num" />
</Columns>
</asp:GridView>
In my backend I have the code that fills the gridview which is working perfectly, also the code that evaluates if the checkbox has been checked.
Now, this is the code of my button that needs to pick the value of all the rows and transfer to another webform, normally I have done it with a "buttonfield" with "OnSelectedIndexChanged" but I can I do it with a checkbox?
protected void Button2_Click(object sender, EventArgs e)
{
List<string> test = new List<string>();
foreach (GridViewRow gridViewRow in GridView2.Rows)
{
if (((CheckBox)gridViewRow.FindControl("cbSelected")).Checked)
{
string ejecutivoId = ((Label)gridViewRow.FindControl("Poliza")).Text;
test.Add(ejecutivoId);
}
}
if (test.Count > 0)
{}
Yes you can.
Fills the gridview and do the same as you did with Button Click, but with theOnCheckedChanged event.

How to show empty data row in gridview control

Here I am working with gridview here is the gidview binding
conn = new SqlConnection(strconnection);
string squery = "sql query";
da = new SqlDataAdapter(squery, conn);
ds = new DataSet();
da.Fill(ds, "tbl1");
GridView1.DataSource = ds;
GridView1.DataBind();
This the gridview control asp code
<asp:GridView ID="GridView1" runat="server" GridLines="None"
HeaderStyle-CssClass="grid_header"
RowStyle-CssClass="grid_item"
RowStyle-Height="30px"
AutoGenerateColumns="false" Width="775px" EmptyDataText="Empty">
<Columns>
<asp:BoundField HeaderText="Order Code" DataField="ordercode"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="Transation Code" DataField="transcode"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Plan Name" DataField="product"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Plan Started" DataField="Start_d"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Plan Ending" DataField="End_d"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:CheckBoxField HeaderText="Payed" DataField="Payed"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Pay Date" DataField="PayDate"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Payed Amt" DataField="amtpaid"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Pay Amt" DataField="PayAmount"
HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
</Columns>
</asp:GridView>
This is result emp like this
Header1 header2 header3 header4
1 asdas 22sdas asdasda
2 sasa asdasas
3 asdas
4 asdasas
like this result
Here my problem is I want show the NULL places just show the "EMPTY" Message. How can I do that?
You can use NullDisplayText="EMPTY" NullDisplayText Attribute
<asp:BoundField DataField="transcode"
NullDisplayText="EMPTY"
HeaderText="header2"/>
If what is coming from your DB is not NULL but an empty string, you will need to use template field
<asp:TemplateField HeaderText="header2">
<ItemTemplate>
<%# Eval("transcode").ToString() == "" ? "EMPTY" : Eval("transcode").ToString() %>
</ItemTemplate>
</asp:TemplateField>
Check out BoundField.NullDisplayText
Gets or sets the caption displayed for a field when the field's value
is null.
Sometimes a field's value is stored as null in the data source. You can specify a custom caption to display for fields that have a null value by setting the NullDisplayText property.
In your case, you can use it like;
<asp:BoundField NullDisplayText="EMPTY" HeaderText="Order Code" DataField="ordercode" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
Since you didn't write your sql query, as an alternative, you can use ISNULL functions for your columns.
Replaces NULL with the specified replacement value.
For example;
SELECT ISNULL(Column1, "EMPTY")
Since you asked, How to change null text color? You can use RowDataBound event for this process. For example;
<asp:GridView ID="gridview1" runat="server" OnRowDataBound="RowDataBound">
</asp:GridView>
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Use for loop for based all rows.
if(e.Row.Cells[i].Text == "EMPTY")
e.Row.Cells[i].BackColor = Color.Red;
}
}
You just need to add :
NullDisplayText="Empty"
Like:
<asp:BoundField HeaderText="Order Code" DataField="ordercode" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" NullDisplayText="Empty" />

Nested GridView Problems

I am trying to nest one gridview within another, but I cannot get the data to populate in the second grid view. I am getting an error when trying to set the data source of the second data grid (saying it is null). Can anyone help?
Here is the aspx page:
<div id="divSource" runat="server" align="center">
<asp:GridView ID="Source" runat="server" AutoGenerateColumns="False" DataKeyNames="sourceLineItem" CSSClass="viewSourceGrid" OnRowDataBound="PopulateDateCodes">
<Columns>
<asp:TemplateField InsertVisible="False" HeaderStyle-Width="70px">
<ItemTemplate>
<asp:Label CssClass="sourceHeader" runat="server" Text= '<%# "Source: " + (Container.DataItemIndex + 1).ToString() %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="nfdBroker" HeaderText="NFD/Broker" InsertVisible="False" ReadOnly="True" SortExpression="nfdBroker" />
<asp:BoundField DataField="locationDescription" HeaderText="Material Location" SortExpression="materialLocation" />
<asp:BoundField DataField="origPkg" HeaderText="Original Packaging?" SortExpression="origPkg" />
<asp:BoundField DataField="oemCC" HeaderText="OEM C of C? " InsertVisible="False" ReadOnly="True" SortExpression="oemCC" />
<asp:BoundField DataField="minBuyQty" HeaderText="Minimum Buy Qty" SortExpression="minBuyQty" />
<asp:BoundField DataField="deliveryInfo" HeaderText="Delivery" SortExpression="delUOM" />
<asp:TemplateField InsertVisible="False" HeaderText="Date Codes" >
<ItemTemplate>
<asp:GridView ID="DateCodeGrid" runat="server" InsertVisible="False" DataKeyNames="dateCode" CSSClass="viewSourceGrid" >
<Columns>
<asp:BoundField DataField="dateCode" SortExpression="dateCode">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and then here is the code behind:
public partial class Controls_ViewSource : System.Web.UI.UserControl
{
//Set the Source Line Item
public int SourceLineItem { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.SourceLineItem = SourceLineItem;
RequestDB db = new RequestDB();
DataSet sources = db.GetSource(int.Parse(Request.QueryString["requestNumber"]), SourceLineItem);
Source.DataSource = sources;
Source.DataBind();
}
protected void PopulateDateCodes(object sender, GridViewRowEventArgs e)
{
RequestDB db = new RequestDB();
int index = e.Row.RowIndex;
GridView gv = (GridView)Source.Rows[0].FindControl("DateCodeGrid");
//int sourceLineItem = int.Parse(Source.DataKeyNames[0].ToString());
//Response.Write(Source.DataKeyNames[0].ToString());
DataSet dateCodes = db.GetDateCodes(71);
gv.DataSource = dateCodes;
gv.DataBind();
}
}
You would need to find the nested grid view in the row that is being data bound:
GridViewRow row = e.Row;
You need to make sure you are only doing this for data rows, not header or footer rows:
if(row.RowType == DataControlRowType.DataRow)
{
// Find the nested grid view
GridView nested = (GridView)row.FindControl("DateCodeGrid");
// The rest of your code for binding the nested grid view follows here
}

Categories