Intro
I'm trying to add a custom row on every 25th row. (25, 50, 75...) The custom row should be like
(start sale button)(create customer
button)(textbox)(textbox)(textbox)(textbox)(textbox)
Essentially mimicking the example gridview's format below. The reason for this is to quickly add a customer and do something with them. Moving forward once the information is inside the textboxes won't be an issue.
Using:
ASP.net
C#
The Problem
I've come across a lot of different examples for doing this, but none where I add a row that's different from the item templates that are already there. In this case having a textbox on the 25th row instead of a label. I'm not sure if this is possible or where to begin with this.
Declaration of the gridview
<asp:GridView ID="grdCustomersSearched" runat="server" AutoGenerateColumns="false" OnRowCommand="grdCustomersSearched_RowCommand" AllowPaging="True" PageSize="25" OnPageIndexChanging="grdCustomersSearched_PageIndexChanging" >
<Columns>
<asp:TemplateField HeaderText="Sale">
<ItemTemplate>
<asp:LinkButton ID="lbtnStartSale" CommandName="StartSale" CommandArgument='<%#Eval("CustomerId") %>' Text="Start Sale" runat="server">Start Sale</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Profile">
<ItemTemplate>
<asp:LinkButton ID="lbtnViewCustomer" CommandName="ViewProfile" CommandArgument='<%#Eval("CustomerId") %>' Text="View Profile" runat="server">View Profile</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Number">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("CustomerId") %>' ID="key"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("firstName") + " " + Eval("lastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Address">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("primaryAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone Number">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("primaryPhoneNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No current customer data, please search for a customer
</EmptyDataTemplate>
</asp:GridView>
Codebehind for populating gridview
The following code gathers the data that is needed and populates the gridview.
protected void btnCustomerSearch_Click(object sender, EventArgs e)
{
//Looks through database and returns a list of customers
//based on the search criteria entered
SweetShopManager ssm = new SweetShopManager();
c = ssm.GetCustomerfromSearch(txtSearch.Text);
//Binds the results to the gridview
grdCustomersSearched.Visible = true;
grdCustomersSearched.DataSource = c;
grdCustomersSearched.DataBind();
}
Example of the gridview
This can be done in the RowCreated event of a GridView. This works best with AutoGenerateColumns set to false.
int rowIndex = 0;
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//the amount of rows in between the inserted rows
int rowsInBetween = 3;
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow && rowIndex % (rowsInBetween + 1) == 0 && rowIndex > 0)
{
//create a new row
GridViewRow extraRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
extraRow.BackColor = Color.Green;
//add one cell with text
TableCell cell1 = new TableCell();
cell1.Text = "ExtraRow";
extraRow.Cells.Add(cell1);
//add another one with a textbox and column spanning
TableCell cell2 = new TableCell();
cell2.ColumnSpan = GridView1.Columns.Count - 1;
TextBox tb1 = new TextBox();
tb1.Text = "Inserted TextBox";
cell2.Controls.Add(tb1);
extraRow.Cells.Add(cell2);
//add the new row to the gridview
GridView1.Controls[0].Controls.AddAt(rowIndex, extraRow);
//extra increment the row count
rowIndex++;
}
rowIndex++;
}
Related
I have seen multiple posts regarding this subject and the answer is pretty much the same. To set the selecteditem programmatically use the following code:
DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
My scenario is slightly different. I'm trying to set the value of a dropdownlist in a gridview.
I am able to populate the dropdownlist, but not able to set the selecteditem or selectedindex.
Gridview
<asp:GridView ID="gvSubject" runat="server"
CssClass="table table-striped clientTblEnabled"
OnRowDataBound="gvSubject_RowDataBound"
AutoGenerateColumns="false"
OnPreRender="gvSubject_PreRender"
GridLines="Both" PageSize="50">
<Columns>
<asp:TemplateField HeaderText="Subject Date">
<ItemTemplate>
<asp:Label ID="lblSubjectDate" runat="server" Text='<%# Bind("SubjectDateTime", "{0:MM/dd/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject">
<ItemTemplate>
<asp:Label ID="lblSubject" runat="server" Text='<%# Bind("SubjectDesc") %>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddlSubject" runat="server" CssClass="input-xlarge controls"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Results found
</EmptyDataTemplate>
</asp:GridView>
Populate dropdownlist
protected void gvSubject_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlSubject = e.Row.FindControl("ddlSubject") as DropDownList;
if (ddlSubject != null)
{
DataSet ds = GetControlData("ddlSubject");
ddlSubject.DataSource = ds.Tables[8];
ddlSubject.DataTextField = "SubjectDesc";
ddlSubject.DataValueField = "SubjectID";
ddlSubject.DataBind();
ddlSubject.Items.FindByValue((e.Row.FindControl("lblSubject") as Label).Text).Selected = true;
}
}
}
lblSubject is populated by another query in GetControlData().
When the debugger gets to the ddlSubject.Items.FindByValue code, I get a NullReferenceException even though lblSubject has a value.
I wonder if I need to change the gridview event for which I'm loading the data.
I think what you're probably after here is FindByText, not FindByValue. The value of the item will be the SubjectID column, whereas the text of the item will match the text of the label.
I have a gridview which I populate from the list data. every row in the gridview has a text box. there is one row within the gridview which I want a dropdown control rather then the textbox. I can't figure out how to change the textbox to dropdown control from a row in the grid.
My gridview below:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" style="width:100%;" ShowHeader="false"
CellPadding="3" BackColor="White" ForeColor="Black" Font-Bold="false" GridLines="None"
RowStyle-CssClass="GridRow">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_ItemID" runat="server" Text='<%# Eval("GroupItemTypeID") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_ItemCode" runat="server" Text='<%# Eval("GroupItemTypeCode") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="310px" >
<ItemTemplate>
<asp:Label ID="lbl_ItemValuesName" runat="server" Text='<%# Eval("ControlName") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="245px">
<ItemTemplate>
<asp:TextBox ID="txtPrice" runat="server" CssClass="form-control"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-CssClass="RowWid">
<ItemTemplate>
<asp:Label ID="lbl_IsPercentbased" runat="server" Text='<%# Eval("PercentBasedText") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="70px">
<ItemTemplate>
<asp:CheckBox ID="ChkIsPercent" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind
private void GetItemValues()
{
List<Entities.ItemValues> IValues = new List<Entities.ItemValues>();
IValues = BLL.PriceGroupItemValues.GetAllPriceGroupItemValues();
GridView1.DataSource = IValues;
GridView1.DataBind();
}
You can go for this approach in your case :
Put a TextBox and DropDown both in that TemplateField where your TextBox is currently, and set Visible=false in your Dropdown.
Now in your code-behind, you can use the GridView.RowDataBound event to alternatively display the TextBox or the Dropdown whenever and wherever you require. Something like this :
public void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// set Dropdown visible = true/false as per your requirement.
}
else
{
// set Textboxvisible = true/false as per your requirement.
}
}
This event will fire up for ear row in your GridView and based on the condition that is specified, you can manipulate which control you want to show in that particular row. You can find the dropdown control like this :
foreach (GridViewRow row in GridView1.Rows)
{
categoryName = ((DropDownList)row.FindControl("ddlCategoryName"));
}
Hope this helps.
Use a placeholder control instead of a textbox and programmatically add either a textbox or DDL to the Placeholder control in the rowdatabound event based on your criteria
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
DataKeyNames="user_id">
<Columns>
<asp:BoundField DataField="user_id" HeaderText="user_id" ReadOnly="True" InsertVisible="False"
SortExpression="user_id"></asp:BoundField>
<asp:BoundField DataField="user_logon" HeaderText="user_logon" SortExpression="user_logon">
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Sample DataSources
'Primary Datasource to bind to the gridview
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:SomeConnectionString %>'
SelectCommand="select top 10 user_id, user_logon from your_user_table"></asp:SqlDataSource>
'Secondary datasource to bind to the ddl
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString='<%$ ConnectionStrings:SomeConnectionString %>'
SelectCommand="select top 5 user_id, user_logon from your_user_table"></asp:SqlDataSource>
Code behind, based on the whether the user_id is odd or even I place one or the other control:
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ph As PlaceHolder = e.Row.FindControl("PlaceHolder1")
If GridView1.DataKeys(e.Row.RowIndex).Value Mod 2 = 0 Then
Dim tb As New TextBox()
tb.Enabled = False
tb.Text = CType(e.Row.DataItem, DataRowView)("user_logon")
ph.Controls.Add(tb)
Else
Dim ddl As New DropDownList()
ddl.DataSourceID = SqlDataSource2.ID
ddl.DataTextField = "user_logon"
ddl.DataValueField = "user_id"
ph.Controls.Add(ddl)
ddl.DataBind()
End If
End If
End Sub
EDIT based on your Comments:
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ph As PlaceHolder = e.Row.FindControl("PlaceHolder1")
If CType(e.Row.DataItem, DataRowView)("GroupItemTypeID") ="SUBFREQ" Then
Dim ddl As New DropDownList()
ddl.DataSourceID = <substitute your requirements>
ddl.DataTextField = ...
ddl.DataValueField = ...
ph.Controls.Add(ddl)
ddl.DataBind()
Else
Dim tb As New TextBox()
tb.Enabled = ...whatever...
tb.Text = CType(e.Row.DataItem, DataRowView)("GroupItemTypeID")
ph.Controls.Add(tb)
End If
End If
End Sub
Did some work around with the solution Harvey provided and my existing code. Posting my answer here.
In the gridview, created 2 control and visibility of one control(dropdown) to false
<asp:TemplateField ItemStyle-Width="245px">
<ItemTemplate>
<asp:TextBox ID="txtPrice" runat="server" CssClass="form-control"></asp:TextBox>
<asp:DropDownList ID="ddFrequencyBilling" runat="server" CssClass="form-control" Visible="false"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
In the RowDataBound event of the gridview
if (e.Row.RowType == DataControlRowType.DataRow)
{
//foreach( GridViewRow row in GridView1.Rows)
//{
String ItemCode = (e.Row.FindControl("lbl_ItemCode") as Label).Text;
if ( ItemCode == "SUBFREQ")
{
List<Entities.ItemValues> PGItemValues = new List<Entities.ItemValues>();
TextBox TextBoxtemp = ((TextBox)e.Row.FindControl("txtPrice"));
TextBoxtemp.Visible = false;
Label lbel = ((Label)e.Row.FindControl("lbl_IsPercentbased"));
lbel.Visible = false;
CheckBox chk = ((CheckBox)e.Row.FindControl("ChkIsPercent"));
chk.Visible = false;
DropDownList dd1 = ((DropDownList)e.Row.FindControl("ddFrequencyBilling"));
dd1.Visible = true;
PGItemValues = BLL.PriceGroupItemValues.GetItemValueOnCode(ItemCode, 1);
dd1.DataSource = PGItemValues;
dd1.DataTextField = "IValue";
dd1.DataValueField = "ItemCode";
dd1.DataBind();
}
//}
}
and yes it worked. Thanks alot guys.
I'm updating the row in a grid view as well as the DataTable using the Rowcommand event but an exception occurs while assigning the values to the row in the DataTable.
protected void grduser_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
DataTable dt = (DataTable)ViewState["dtable"];
Int32 index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = grduser.Rows[index];
// after this statement the exception occurs!
dt.Rows[row.DataItemIndex]["userid"] = ((TextBox)(row.Cells[0].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["username"] = ((TextBox)(row.Cells[1].FindControl("txtuname"))).Text;
dt.Rows[row.DataItemIndex]["usertype"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["email"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["salary"] = ((TextBox)(row.Cells[4].FindControl("txtsalary"))).Text;
grduser.EditIndex = -1;
ViewState["dtable"]=dt;
grduser.DataSource = (DataTable)(ViewState["dtable"]);
grduser.DataBind();
}
}
// The grid view source below.
<asp:GridView ID="grduser" runat="server" AutoGenerateColumns="False"
onrowediting="grduser_RowEditing" onrowupdating="grduser_updateRow"
>
<Columns>
<asp:BoundField HeaderText="Id" DataField="userid"/>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtuname" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="User Type" DataField="usertype" />
<asp:BoundField HeaderText="Email" DataField="email" />
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="txtsalary" runat="server" Text='<%# Bind("salary") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("salary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Could you try this.
protected void grduser_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
DataTable dt = (DataTable)ViewState["dtable"];
Int32 index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = grduser.Rows[index];
dt.Rows[row.DataItemIndex]["userid"] = row.Cells[0].Text;
dt.Rows[row.DataItemIndex]["username"] = ((TextBox)(row.FindControl("txtuname"))).Text;
dt.Rows[row.DataItemIndex]["usertype"] = row.Cells[2].Text;
dt.Rows[row.DataItemIndex]["email"] = row.Cells[3].Text;
dt.Rows[row.DataItemIndex]["salary"] = ((TextBox)(row.FindControl("txtsalary"))).Text;
grduser.EditIndex = -1;
ViewState["dtable"]=dt;
grduser.DataSource = (DataTable)(ViewState["dtable"]);
grduser.DataBind();
}
}
You are getting that error because your e.CommandArgument is not a valid row number from your GridView. For instance, your grduser has 3 rows and your e.CommandArgument is set to 3. This will throw a System.ArgumentOutOfRangeException because the grduser.Rows is 0-based and you are most likely setting it starting with 1.
How can I select all data from GridViews current row..
I have a column for edit link in GridView. When "Edit" link button is clicked, I want to use that selected row's data. I am trying the following code, but it's returning me with an empty value
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = -1;
GridViewRow gvRow = gv.Rows[ e.NewEditIndex];
string selectedID = gvRow.Cells[3].Text;
}
<asp:GridView runat = "server" ID="gvRange0" SkinID="gridView" AutoGenerateColumns="False"
AllowSorting="True" OnRowCancelingEdit="gvRange_RowCancelingEdit" OnRowDeleting="gvRange_RowDeleting"
OnRowEditing="gvRange_RowEditing" OnRowUpdating="gvRange_RowUpdating"
Width="684px" OnRowDataBound="gvRange_RowDataBound"
DataMember="DefaultView" OnPageIndexChanged="gvRange_PageIndexChanged"
OnPageIndexChanging="gvRange_PageIndexChanging" OnSorting="gvRange_Sorting" DataKeyNames = "RANGE_ID"
OnSelectedIndexChanged="gvRange_SelectedIndexChanged" Height="65px" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<ControlStyle Width="2px" />
<asp:LinkButton ID="lnkDelete0" runat="server" CssClass="lnk"
CausesValidation="False" CommandName="Delete"
Text="Delete" Visible="false"></asp:LinkButton>
<asp:CheckBox runat="server" ID="chkSelect" CssClass="lbl" Text="" AutoPostBack="False" OnCheckedChanged="chkSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<controlStyle width="2px" />
<asp:LinkButton ID="lnkEdit" runat="server" CssClass="lnk" CausesValidation="False" CommandName="Edit"
Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="5px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Ranges" SortExpression="Sort_Ranges">
<ControlStyle Width="5px" />
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"Min_Age") %>
<%# CheckNull(DataBinder.Eval(Container.DataItem,"Max_Age")) %>
</ItemTemplate>
<%-- <ItemTemplate>--%>
<%--<asp:Label ID="lblStageName" CssClass="lbl" runat="server" Text='<%# Bind("Age_Range") %>' Width="1px"></asp:Label>--%>
<%-- </ItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Range ID">
<ItemTemplate><%#DataBinder.Eval(Container.DataItem,"RANGE_ID") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There are 4 columns in the GridView. One contains check box, second is link button for edit, third databound with some value, fourth is the column which I want to use to get some values from database(that is primary key there) and this column is hidden.
sometime in gridview cell create child controls.
you can try this code. may be solve it.
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = -1;
GridViewRow gvRow= (GridViewRow)(((Button)e.CommandSource).NamingContainer);
foreach (TableCell Tc in gvRow.Cells)
{
//if you are not getting value than find childcontrol of TabelCell.
string sss = c.Text;
foreach (Control ctl in Tc.Controls)
{
//Child controls
Label lb = ctl as Label;
string s = lb.Text;
sb.Append(s + ',');
}
}
}
I've noticed that you say you need to access the 4th column but you're using gvRow.Cells[3].Text;
The indexing in the Cell object is done from 1, so if you need to access the 4th row in the grid view try this:
string selectedID = gvRow.Cells[4].Text;
EDIT:
Could you please confirm two things for me
1)When you click on lnkEdit is the GridView1_RowEditing event being raised?
2)If yes, is the e.NewEditIndex value always showing up as '0'?
Attempt clicking the edit link on different rows, is the result always '0'?
I have a GridView with an anonymous type. I need to check the value in a cell and highlight that cell if a condition is met. The problem is, I always get an empty string when I try to pull data out of the row's cells. I have successfully highlighted all the cells and I have checked in the Visual Studio 2010 debugger and confirmed that the Row has data (the row's DataItem has the values I need). This is happening on a PostBack, I'm not sure if that's a problem or not.
Here is the code and solutions I've tried:
protected void grvValidCourses_RowDataBound(Object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
String str = e.Row.Cells[6].Text.ToString(); // empty string
Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null
DataRowView rowView = (DataRowView)e.Row.DataItem; // exception about casting anonymous type
What's going on here? Why can't I get data from the cells?
Markup for GridView:
<asp:GridView ID="grvValidCourses" runat="server" Width="790px" OnRowCancelingEdit="grvValidCourses_RowCancelingEdit"
OnRowEditing="grvValidCourses_RowEditing" OnRowUpdating="grvValidCourses_RowUpdating" OnRowDeleting="grvValidCourses_RowDeleting"
AutoGenerateColumns="False" OnSelectedIndexChanged="grvValidCourses_SelectedIndexChanged"
OnRowDataBound="grvValidCourses_RowDataBound" >
<Columns>
<asp:CommandField ShowEditButton="True" EditText="Edit" UpdateText="Update |" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbnDelete" runat="server" CausesValidation="False" CommandName="Delete"
Text='<%# (Eval("active") == null ? "Delete" : ((Eval("active").ToString() == "0" ? "Restore" : "Delete"))) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" SelectText="Details" />
<asp:TemplateField HeaderText="Training Name" SortExpression="coursename">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("coursename") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("coursename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ttNo" HeaderText="#" SortExpression="ttNo" ReadOnly="True" />
<asp:TemplateField HeaderText="Course Date" SortExpression="startDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("startdate", "{0:M/d/yyyy}") %>'></asp:TextBox>
<asp:CalendarExtender ID="TextBox3_CalendarExtender" runat="server" Enabled="True"
TargetControlID="TextBox3" Format="M/d/yyyy">
</asp:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("startdate", "{0:M/d/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiry Date" SortExpression="endDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("enddate", "{0:M/d/yyy}") %>'></asp:TextBox>
<asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True"
TargetControlID="TextBox1" Format="M/d/yyyy">
</asp:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEndDate" runat="server" Text='<%# Bind("enddate", "{0:M/d/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>No valid courses found.</EmptyDataTemplate>
</asp:GridView>
UPDATE: I've been trying all your suggestions, but I get exceptions or nulls or empty strings. I've even re-created the problem in a simpler example and still can't figure it out! I'll keep trying though, and I appreciate any new suggestions.
Part 1 - Missing Text
You are probably getting blank values in the first part due to a need to access a child control:
String str = ((DataBoundLiteralControl)e.Row.Cells[6].Controls[0]).Text;
To see if your cells have any values in debug mode (check text in debug output window):
void grvValidCourses_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Diagnostics.Trace.WriteLine(e.Row.Cells.Count);
foreach (TableCell c in e.Row.Cells)
{
System.Diagnostics.Trace.WriteLine(c.Text);
}
}
}
Part 2 - Missing Control
This is wrong:
Label lbl = (Label) grvValidCourses.FindControl("lblEndDate"); // null
You can't search the gridview for a row's control. You need to search the row.
Label lblProductOptionGrpName = (Label)e.Row.FindControl("lblProductOptionGrpName");
Part 3 - Accessing DataItem
DataBinder.Eval(e.Row.DataItem, "ColumnName")
Finally, I'm not sure what you're doing with your anonymous types, but you may need to check the contents before accessing properties:
if(MyControl.GetType() == typeof(HyperLink))
{
HyperLink TestLink = (HyperLink)MyControl;
TestLink .Visible = false;
}
Okay I finally figured out what is going on! I can only access the bound field through the row (e.g. e.Row.Cells[index].Text). So I used the bound field to get the ID of my item, then found the item in the database, got the date, compared it, and highlighted the row's cells. Not the most efficient way, but it works.
Code from simple sample:
Grid View Front End
<asp:GridView ID="gdv" runat="server" AutoGenerateColumns="True" OnSelectedIndexChanged="gdv_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Select" />
<asp:BoundField DataField="id" HeaderText="#" ReadOnly="True" />
<asp:TemplateField HeaderText="CategoryName" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" >
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("desc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblSelected" runat="server" Text="No row selected"></asp:Label>
Code Behind Page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
var qry = ctx.Categories
.Select(c => new {
id = c.CategoryID,
name = c.CategoryName,
desc = c.Description,
});
gdv.DataSource = qry;
gdv.DataBind();
}
}
protected void gdv_SelectedIndexChanged(object sender, EventArgs e) {
selectRow();
}
private void selectRow() {
GridViewRow row = gdv.SelectedRow;
String strId = row.Cells[1].Text; // Bound Field column
lblSelected.Text = strId;
// use ID to get object from database...
}
If you can post markup, then it will be possible to figure out your issue.
You are doing this in wrong way,
this DataRowView rowView = (DataRowView)e.Row.DataItem;
should be DataRow rowView = ((DataRowView)e.Row.DataItem).Row;
protected void gridPanne_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
Label label = (Label)e.Row.FindControl("Label4");
lbl.Text += " ** / ** "+label.Text;
}
}