How can i add cell in specific column for each row in gridview,i want to use RowCreated Events.
i have gridview that has 3 columns (ProductName, Price, Count) i get (ProductName, Price) from database, and i want to add value for (count), ex: (Kitkat, 2$ ) i want to add number 5 to (count) column, i want to handle this operation when each row created.
Thanks
Since you haven't shown your markup, I'm going to assume (based on your comment) that the first two columns are <BoundFields>. If that's the case, I would add a third column as a <TemplateField>, place a Label in it, and use the RowDataBound event to add the correct number to the Label.
Here is what the markup would look like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:Label ID="countLbl" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code-behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label countLbl = (Label)e.Row.FindControl("countLbl");
//Your logic for what number to use should go here, I'm just defaulting to 5.
countLbl.Text = "5";
}
Related
Developing In: c# asp.net web forms 4.5
I've looked at many posts and all of them made a hyperlink with either datafield in it, by datatextfield() or similar.. or called eval from backend.
I didn't do neither of that, so I tried doing every thing I could but it didn't work. CommandField, HyperLinkeField, HyperLink, .. all didn't work.
The main point is that the data comes out fine, but I can't seem to make it hyperlink. Is it impossible to have a hyperlink this way?
I'm trying to make a hyperlink on gridview.
The thing is that the column I'm trying to make the hyperlink
doesn't get data from the datafield.
It gets it by onRowDataBound method of gridview
something like this..
<asp:GridView runat="server" ID="someGrid" CellPadding="10"
DataKeyNames="idx" AutoGenerateColumns="false"
selectMethod="someGrid_GetData" ItemType="someTable"
updateMethod="someGrid_UpdateItem" AutoGenerateEditButton="true"
deleteMethod="someGrid_DeleteItem" AutoGenerateDeleteButton="true"
onRowDataBound="someGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="thing1" HeaderText="thing1" />
<asp:BoundField DataField="thing2" HeaderText="thing2"/>
<asp:DynamicField DataField="poDate" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="vendor" />
<asp:CommandField HeaderText="sku" ShowSelectButton="true" SelectText="{0}" ButtonType="Link"/>
</Columns>
</asp:GridView>
and on the code background, it goes like this..
protected void soGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
using (soAction soa = new soAction())
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string s = e.Row.Cells[2].Text;
string thin1 = soa.get_thin1(s);
e.Row.Cells[4].Text = thin1;
string thin2 = soa.get_thin2(s);
e.Row.Cells[5].Text = thin2;
}
}
}
Thanks!
edit :
Maybe I wasn't clear about what is the challenge here..
normally the examples use dataTextField property in gridview but
I cannot use the dataTextField property because I'm binding the data
depending on the model rendered afterwards.
I'm doing this because I wanted to set the itemType into someTable
so I can use the updatemethod and deleteMethod of the asp.net.
Just use a TemplateField like this, you can do anything you want in a templatefield, combine data from multiple columns, create controls, whatever:
<Columns>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/custom-formatting/using-templatefields-in-the-gridview-control-cs
So I got a ASPxGridView with few VisibleColumns which shows records of percentage value. I need for some of that columns, ie. VisibleColumns[1] , apply format of theirs value. These records are save in DB like 83.79000. These columns got rounded values in 2 decimal places.
So my first Question: how could i show these values in format like 83.79?
Second Q: when exactly I should do that? If you look at this code snippet, I guess after Databind i got a records in GridView but how modify them and save them to that GridView?
ASPxGridViewMain.DataSource = this.DataSource;
ASPxGridViewMain.DataBind();
Third Q: in purpose of Debugg, how i can see the values of that column?
EDIT: My ASPXGridView like:
<dxwgv:GridViewDataTextColumn FieldName="TRS" VisibleIndex="1" Caption="Hodnota TRS">
</dxwgv:GridViewDataTextColumn>
You can use EditPropertiesBase.DisplayFormatString property to show values in your format. You don't need to modify your records. You can just add this to your aspx code:
<dxwgv:GridViewDataTextColumn FieldName="TRS" VisibleIndex="1" Caption="Hodnota TRS">
<PropertiesTextEdit DisplayFormatString="#.00" />
</dxwgv:GridViewDataTextColumn>
You should use DataFormatString the msdn link is below
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring%28v=vs.110%29.aspx
You should use bound fields to use data format.
<asp:BoundField DataField="amount" HeaderText="amount"
ReadOnly="True" SortExpression="amount" DataFormatString="{0:n3}" />
Edit 1
If you don't want to use BoundField then you should use RowDataBound event of gridview. Get the cell and format as you like.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//get you cell here
//put the number format here.
}
}
You can use bound columns in GridView and specify DataFormatString. Like below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="C1" HeaderText="Display Column Name" DataFormatString="{0:0.00}" />
</Columns>
</asp:GridView>
i have to display data like this in grid view:
the problem is that:
for each unique set of manufacturer , model and type, the number of values associated with watts column changes ... that is, there is no fixed numbers of values in this single column. how can i display such a format in my grid view? Kindly help ...
Could you just place another (nested) gridView in that column? You'd have to use a template column in your parent grid, but then for each row, the number of records in that column could be different.
<asp:GridView ID="parentGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Manufacturer" />
<asp:BoundField DataField="ModelNo" HeaderText="Model No" />
<asp:BoundField DataField="Type" />
<asp:TemplateField HeaderText="Watts">
<ItemTemplate>
<asp:GridView ID="nestedWattsView" runat="server" AutoGenerateColumns="true" ShowHeader="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have the below code where I have some conditions because of which I have to hide the row from showing to end user. "ShowRow" is a boolean value that gets set in GetUnitOfMeasure function (not copied here) based on these conditions.
There are some real conditions which is forcing me to hide. I tried to include them while building data source but I have to hide it before display.
Problem I am facing is the paging is not getting refreshed based on total rows shown at the end. For example, if I have TOTAL of 200Rows in the grid and only 2 rows needs to be shown and if these 2 rows are found in paging 3, then when page is loaded it still shows paging 1 2 3 4 and 3rd page has the valid 2 rows.
Please suggest.I have also used "onDataBound" against gridview (not copied here) but I just hide some columns here.
ASPX page
<asp:GridView ID="SearchResults" runat="Server" AutoGenerateColumns="false" EnableViewState="false"
AllowPaging="true" PageSize="50" OnDataBound ="SearchResults_DataBound" OnRowDataBound="SearchResults_RowDataBound">
<RowStyle CssClass="EvenRow" />
<AlternatingRowStyle CssClass="OddRow" />
<Columns>
<asp:TemplateField meta:resourceKey="UmSellField">
<ItemStyle CssClass="alpha" />
<HeaderStyle CssClass="alpha" />
<ItemTemplate>
<asp:Label ID="UmSellLabel" runat="server" EnableViewState="false" Text='<%# GetUnitOfMeasure(Container.DataItem,false) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
codebehind
protected void SearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
e.Row.Visible = showRow;
e.Row.Cells[0].Visible = showRow;
}
}
Background
I have a GridView where input in Column1 depends on the input Column2.
If a user enters a N in Column2 the system will put a Y in Column1.
The Validatons are implemented using Regexs and Custom validation. I would prefer a validation solution that doesn't use JavaScript.
|Column1| Column2|
__________________
| Y | N
__________________
|N | Y
__________________
|N | N
Question
How can I validate these entries in the Gridview without using JavaScript?
Why not use two radio buttons?
<asp:templatefield>
<itemtemplate>
<asp:radiobutton id="rbYes" runat="server" groupname="YesNo" text="Yes" />
<asp:radiobutton id="rbNo" runat="server" groupname="YesNo" text="No" />
</itemtemplate>
</asp:templatefield>
You could use radio buttons and the 'Template' column feature of a GridView. The GridView markup would look like this:
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false"
OnRowDataBound="gvTest_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Column 1">
<ItemTemplate>
<asp:RadioButton ID="rbSelect1" runat="server" Text="" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column 2">
<ItemTemplate>
<asp:RadioButton ID="rbSelect2" runat="server" Text="" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The trick then would be to correctly set the 'GroupName' property of each radio button so that each row of the resulting grid is treated as a single radio button group by the browser. That's where the 'OnRowDataBound' handler specified on the grid comes into play. The definition of the 'gvTest_RowDataBound' handler method could be something like:
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rb1 = (RadioButton)e.Row.FindControl("rbSelect1");
RadioButton rb2 = (RadioButton)e.Row.FindControl("rbSelect2");
rb1.GroupName = rb2.GroupName = string.Format("Select_{0}", e.Row.RowIndex);
}
}
By appending the row index to the group name to both of the radio buttons in each row you're going to ensure that the browser will treat them as a group and only allow the selection of one value per row. The result would look something like this: