I have a standard ASP.NET GridView and I'd like the first column (a emplateField) to be rendered as <th>, or in ASP.NET terms, I'd like to set it to the GridView RowHeaderColumn property. But that property is looking for the name of a DataItem (from a BoundColumn).
How can I render my TemplateField with <th> tags?
Finally found a workaround for this. I am not sure if this code has anything to do with good ASP.NET practices, but it does the trick:
public class FirstColumnHeaderGridView : GridView
{
protected override void InitializeRow(GridViewRow row, DataControlField[] fields)
{
DataControlFieldCell cell = new DataControlFieldHeaderCell(fields[0]);
DataControlCellType header = DataControlCellType.DataCell;
fields[0].InitializeCell(cell, header, row.RowState, row.RowIndex);
row.Cells.Add(cell);
DataControlField[] newFields = new DataControlField[fields.Length - 1];
for (int i = 1; i < fields.Length; i++)
{
newFields[i - 1] = fields[i];
}
base.InitializeRow(row, newFields);
}
}
Let me explain what is going on here. We are creating a special type of GridView, that will render its first column using <th> tags no matter how this column is created. For this we are overriding the InitializeRow method. This method basically configures cells for the row. We are handling the first cell, and let standard GridView take care of the rest.
The configuration we are applying to the cell is fully taken from the GridView implementation and is enough for the cell to be rendered with <th> tag instead of <td>.
After that workaround the usage is absolutely standard - register our class as a server control and use it as usual GridView:
<%# Register Assembly="WebApplication1" Namespace="WebApplication1" TagPrefix="wa1" %>
...
<wa1:FirstColumnHeaderGridView ID="Grid1" runat="server" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
Will be inside th
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
Will be inside td
</ItemTemplate>
</asp:TemplateField>
</Columns>
</wa1:FirstColumnHeaderGridView>
Is this what you mean?
<Columns>
<asp:TemplateField HeaderText="Código" ItemStyle-Width="9%">
<HeaderTemplate>
<asp:Label runat="server" Text="CodigoSAP"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblCodigoSAP" Text='<%# Bind("CodigoSAP") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
I'm almost sure I'm getting the wrong idea, what do you say?
Late to the game, but we needed to set scope="row" in a middle column, not the first. To make it generic, in a derived GridView class I added the following property (similar to the GridView's built-in RowHeaderColumn property):
public int? RowHeaderColumnIndex
{
get { return (int?)ViewState["RowHeaderColumnIndex"]; }
set { ViewState["RowHeaderColumnIndex"] = value; }
}
Then set the scope:
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && RowHeaderColumnIndex.HasValue)
{
e.Row.Cells[RowHeaderColumnIndex.Value].Attributes["scope"] = "row";
}
}
When you place your custom grid just set RowHeaderColumnIndex="0" for the first column, "1" for the 2nd column and so on.
Related
I want to select Particular Cell value and will use to Bind Controls But i am Not able to select Cell Value
Its not retrieving data
ASPX Code
<asp:GridView ID="grdLogedUserDetails" OnRowDeleting="grdLogedUserDetails_RowDeleting" OnRowDataBound="grdLogedUserDetails_RowDataBound"
runat="server" Style="width: 100%; text-align: center" OnRowCommand="grdLogedUserDetails_RowCommand"
class="table table-striped table-bordered" AutoGenerateColumns="false" datakeynames="Ref_ID">
<Columns>
<asp:TemplateField HeaderText="Process No">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("Ref_ID") %>' ID="lblRef_ID"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("CompanyName") %>' ID="lblCompanyName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DivisionName") %>' ID="lblDivisionName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgDel" runat="server" ImageUrl="~/Images/delete.png" AlternateText="Delete" CommandName="Delete" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:ImageButton ID="imgSel" runat="server" ImageUrl="~/Images/edit-icon.png" AlternateText="Select" CommandName="Select" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
C#
protected void grdLogedUserDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow row = grdLogedUserDetails.Rows[rowIndex];
//Access Cell values.
int RefID= int.Parse(row.Cells[0].Text);
string Company= row.Cells[1].Text;
string Division= row.Cells[2].Text;
}
protected void grdLogedUserDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Access Cell values.
var RefId = int.Parse(e.Row.Cells[0].Text); // Error Input string is not in correct Formate
string Comany= e.Row.Cells[1].Text;
string Division= e.Row.Cells[2].Text;
}
}
While Debugging I can see Error Like :
"Input string is not in correct Format" Where i am doing wrong
My Table Structure
________________________________________
Ref_ID| CompanyName | DivisionName |
______|_______________|_________________|
6 | Company | Sales |
8 | Company | Sales |
14 | Company | Sales |
______|_______________|_________________|
RefID i have taken as "int" and CompanyName and DivisionName "string" so where i my input string is in wrong format,
Where i am doing wrong Please suggest me on same
The rowcommand event does not trigger rowindexed change unless you have Command="Select" but you DO HAVE Command="Select". However, you don't have a CommandArugment.
However, since you do have command=Select, then move your code to SelectedIndexChanged. The issue is you can't get/grab the row from the "row" command event since the row has not changed. The rowcommand event fires before SelectedIndexChanged. And as noted, unless you use a special command (delete or select) then SelectedIndexChanged does NOT fire.
So rowindex will NOT have been set in the row command event. However, this will mean that for your select button (and delete) your code will be (can be) in rowindexchanged "if" you use say select (or "delete") for the command. if you use a custom command name, then rowindexedchanged does NOT fire.
You can continue to use rowcommand, but you need the CommandArugment to pass either the PK of the row, or the index of the row.
So you have two choices here. (actually 3).
You can set the value of CommandArugment of that button.
Either to the PK, or you can use this
CommandArgument ='<%# Container.DataItemIndex%>'
I note that in your command settings, you are NOT setting the CommandArugment - you have to set it in your markup. The above expression will return the index into the grid.
You can move your code to rowindexchanged.
However, there is a 3rd choice. You CAN GET the rowindex in rowcommand. So from the depths of hell, you can use this stunning whopper insane of a statement:
If e.CommandName = "MySelect" Then
' now get row.
Dim gvRow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Debug.Print("row index = " & gvRow.DataItemIndex)
Dim MyLabel As Label = gvRow.FindControl("HotelName")
Debug.Print("Value ref id = " & MyLabel.Text)
End If
Note VERY careful:
For the templated fields - we use find control. For non tempalted fields, then you can reference by cell.
Now that super award winning ugly way to get the row in row command?
In C# it will look like this:
{
GridViewRow gvRow = (GridViewRow)(Control)e.CommandSource.NamingContainer;
}
So, you have to use above, or as noted, move your code to SelectedIndexChanged.
Since you only have a select and delete button, then I would suggest SelectedIndexChanged. But if you were to say introduce another button - then you would not want to use Select or Delete, but a custom command name of your choosing (say CommandName="MyViewer"). In that case, then SelectedIndexChanged will not fire. But then you would have a means to determine which button was clicked upon (beyond the need of select and delete commands).
So in summary:
You have about 3+ ways to do this.
Keep in mind that in row command, the selected index event has NOT yet fired.
Keep in mind that if you use a custom row command name, then selected index WILL NOT fire.
Keep in mind that you can pick up the selected row with that award winning ugly line of code.
Keep in mind that you can't use cells() collection for your custom templated columns - so use find control.
Keep in mind that selectedindex in row command event has NOT changed nor fired yet.
Since you ARE using select, then you have a choice of row command event, or selectedindex changed event - they will both fire in your case.
The problem is that the cell does not contain any text, but a Label lblRef_ID. So you need to access the label, not the cell value.
var label = e.Row.FindControl("lblRef_ID") as Label;
var RefId = int.Parse(label.Text);
I have a GridView with two columns and I want to add a third column which will be column A divided by column B. I added a template field but I am getting a divide by zero error. How can I check for zero values to stop the error message?
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCalc" runat="server" >
<%# Convert.ToDecimal(Eval("val1").ToString()) / Convert.ToDecimal(Eval("val2").ToString()) %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Yes, you sure can. You can even do that inline, but that would clutter your markup too much, so I would suggest moving this code to code behind.
protected decimal Calculate(string a, string b)
{
decimal ad = Convert.ToDecimal(a);
decimal bd = Convert.ToDecimal(bd);
if (bd == 0)
{
return 0; // or whatever
}
return ad / bd;
}
To call this:
<asp:Label ID="lblCalc" runat="server" >
<%# Calculate(Eval("val1").ToString(), Eval("val2").ToString()) %>
</Label>
im not getting the valu of specic row in row data bound event , value coming null;
<asp:TemplateField>
<HeaderTemplate>
Today's pos
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl_TodayPos" runat="server" Text='<%# Eval("CurrentPosition") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
aspx.cs code
protected void GrdKeyWord_RowCommand(object sender, GridViewCommandEventArgs e)
{
string value = GrdKeyWord.Rows[rowindex].Cells[5].ToString();
}
The value you are looking for is stored in a label control not in a table cell. Therefore, you need to use FindControl on that row to access the lbl_TodayPos:
Label myLabel = (Label)GrdKeyWord.Rows[rowindex].FindControl("lbl_TodayPos");
string value = myLabel.Text;
If you autogenerate the columns in the gridview, or if you used 'BoundField' (instead of TemplateField) you could use .Cells[]. Because, in this case, you would have gridview rendered as pure html table with table cells.
I have a GridView with some predefined Columns and some generated in code. The idea is that I show Columns according to category, selected by user. I cannot create all Columns and just hide them because I don't know how many Columns I will need. I manage to generate Columns I needed, but the problem starts when I try to remove generated Columns. Situation looks like this:
On first load I see GridView of all categories.
After clicking in ListBox I get result I want. 1 additional Column is created (RemoveAt is not called, because no additional Columns were yet created.).
After clicking in other ListBox Item I still get result I want. Column created last time where deleted and new Column added.
At this point, if I click any other ListBox Item in LicensesCategoriesListBox_SelectedIndexChanged on debugging I see that all of GridView TemplateFields are empty (http://tinypic.com/r/98vdkm/8).
If I comment section gridView.Columns.RemoveAt(i - 1) everything works fine, just Columns keeps generating and generating. Any ideas why all of my TemplateFields, written in my Page becomes empty?
My Page looks like this:
<asp:ListBox ID="licensesCategoriesListBox" runat="server" AutoPostBack="true" OnSelectedIndexChanged="LicensesCategoriesListBox_SelectedIndexChanged" />
<asp:GridView ID="licencesGridView" runat="server" AutoGenerateColumns="False" Caption="Licencijos" DataKeyNames="id" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="Pavadinimas">
<EditItemTemplate>
<asp:TextBox ID="licenceNameTextBox" runat="server" MaxLength="50" Text='<%# Bind("name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newLicenceNameTextBox" runat="server" MaxLength="50" ToolTip="Pavadinimas" />
</FooterTemplate>
<ItemTemplate>
<span><%# Eval("name") %></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kategorija">
<EditItemTemplate>
<asp:DropDownList ID="licenceCategoryDropDownList" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="newLicenceCategoryDropDownList" runat="server" ToolTip="Kategorija">
<asp:ListItem Text="Pasirinkite kategoriją:" />
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<span><%# Eval("category") %></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
FillLicences(ref licencesGridView);
}
}
protected void LicensesCategoriesListBox_SelectedIndexChanged(object sender, EventArgs e) {
FillLicences(ref licencesGridView, licensesCategoriesListBox.SelectedValue); /// Value is ID of category.
}
public void FillLicences(ref GridView gridView, string category = "") {
DataTable dataTable;
ushort categoryId;
if (UInt16.TryParse(category, out categoryId)) {
PutAdditionalColumns(ref gridView, categoryId);
dataTable = sqlCommands.GetLicences(categoryId); /// Returns DataTable [name], [category] and additional fields that I add in PutAdditionalColumns method.
} else {
dataTable = sqlCommands.GetAllLicences(); /// Returns DataTable with only [name], [category]
}
gridView.DataSource = dataTable;
gridView.DataBind();
}
public void PutAdditionalColumns(ref GridView gridView, uint category) {
for (ushort i = (ushort)gridView.Columns.Count; i > 2; i--) { /// Removes columns at the end (created for other category)
gridView.Columns.RemoveAt(i - 1);
}
foreach (var pair in sqlCommands.GetLicencesCategoryAttributes(category)) { /// Takes additional field needed.
TemplateField field = new TemplateField(); /// New empty column.
field.AccessibleHeaderText = pair.Key.ToString();
field.HeaderText = pair.Value;
gridView.Columns.Add(field);
}
}
Any ideas why all of my TemplateFields, written in my Page becomes
empty?
Because each of those fields is considered a Column. You're removing them in the code.
If this is your issue, you need some way to seperate columns defined in the aspx to the ones created in the code.
I'd use DataColumn.ExtendedProperties to do this.
Whenever you add a column from code, add a value to ExtendedProperties, showing that it was created in the code.
//...
TemplateField field = new TemplateField(); /// New empty column.
field.AccessibleHeaderText = pair.Key.ToString();
field.HeaderText = pair.Value;
DataColumn ourNewColumn = gridView.Columns.Add(field);
ourNewColumn.ExtendedProperties.Add("CreatedInCode", true);
//...
And then when you come to delete them, only delete them if they have this property set.
//...
if( gridView.Columns[i - 1].ExtendedProperties.ContainsKey("CreatedInCode") ) {
gridView.Columns.RemoveAt(i - 1);
}
//...
Can I update a gridview column's content after databind? The SQL column is a full name, and I need to display it as last name, first name, not first name last name, as it is now. I know that i can do this in sql, but it seems as though it is going to be kind of a pain (SQL: parse the first, middle and last name from a fullname field). I was just wondering if there is an easier way to do this.
Thanks
Ok, I figured it out... sorry. In the event someone else needs this, I added a OnRowDataBound="MailingList_RowDataBound" event, and within that event handler, I have am going to use the following code (modified to parse the name):
public void MailingList_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Required to ignore the header/footer.
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Get the Person's Name
string name = e.Row.Cells[0].Text;
//Add your JavaScript onClick event link.
e.Row.Attributes.Add("onClick", "LookupEmail('" + name + "')");
e.Row.Cells[0].Text = "Test";
}
}
Changing the "Test" to the correct values.
use itemtemplate
<asp:GridView ID="tset" runat="server">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="label1" runat="server" Text='<%#GetFormattedName(Eval("full_name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and write GetFormattedName method in code behind to return a string however you want,
protected string GetFormattedName(string fullName)
{
// you can split full name here and swap first name, last name
// depending on how you store it in DB
}
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<span>
<%# Eval("FullName").Split(' ')[0]%>
</span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname">
<ItemTemplate>
<span>
<%# Eval("FullName").Split(' ')[1]%>
</span>
</ItemTemplate>
</asp:TemplateField>
This should solve your problem. Assuming the column name is FullName in your database and the first name and surname are seperated with space, you can split those and bind each value to different columns in GridView.