I have a gridview :
<asp:GridView ID="gvReportingListeOF" runat="server" AutoGenerateColumns="false" Visible="false">
<Columns>
<asp:BoundField DataField="cod_wo" HeaderText="N° OF" />
<asp:BoundField DataField="cod_ref" HeaderText="Référence article" />
<asp:BoundField DataField="lbl_article" HeaderText="Désignation article" />
<asp:BoundField DataField="dat_sequence_wo" HeaderText="Séquence" />
<asp:BoundField DataField="wo_qty" HeaderText="Qté prévue" />
<asp:BoundField DataField="qty_revue" HeaderText="Qte revue" />
</Columns>
</asp:GridView>
And I wish changing dynamically the header text with code behind C#. It is possible ? Because they doesn't have ID...
Thanks
Try the above
gvReportingListeOF.Columns[ColumnIndex].HeaderText = "Header text"
You need to write the code in the RowDataBound Event like this.
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "First Column";
e.Row.Cells[1].Text = "Second Column";
}
Related
I have an asp:linkbutton. When it's pressed I want to get a list of DetailID's based on which of the checkboxes in my GridView are checked. I have a lblTesting to see the list of DetailID's that I'm generating. My goal is to get a list of DetailID's that I pass to an edit page.
I have looked at several examples on Stack Overflow but I cannot get any value other than the checkbox is checked.
Here is the link I have at the top of my page:
<span style="float:right;"><asp:Linkbutton ID="getURLLink" runat="server" Text="Edit Die Detail" OnClick="GetLink"></asp:Linkbutton></span>
<asp:Label runat="server" ID="lblTesting"></asp:Label>
Here is my GridView:
<asp:GridView ID="DieListDetailGridView" runat="server" AutoGenerateColumns="false" OnRowDataBound="DieListDetailGridView_RowDataBound" DataKeyNames="DieID" HeaderStyle-HorizontalAlign="Center" HorizontalAlign="Center" ForeColor="#333333" GridLines="Both">
<RowStyle BackColor="#F5F5DC" />
<AlternatingRowStyle BackColor = "White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DieNum" HeaderText="Die Number" />
<asp:BoundField DataField="DieID" HeaderText="Die ID" Visible="false" />
<asp:BoundField DataField="DetailID" HeaderText="Detail ID" Visible="false" />
<asp:HyperLinkField DataTextField="DetailNum" HeaderText="Detail Number" DataNavigateUrlFields="DetailID" DataNavigateUrlFormatString="~/Tooling/DieDetail.aspx?DetailID={0}" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="MaterialType" HeaderText="Material Type" />
<asp:BoundField DataField="Hardness" HeaderText="Hardness" />
<asp:BoundField DataField="MinSpares" HeaderText="Min Spares" />
<asp:BoundField DataField="MaxSpares" HeaderText="Max Spares" />
<asp:BoundField DataField="CurrentSpares" HeaderText="Current Spares" />
<asp:BoundField DataField="DetailNotes" HeaderText="Detail Notes" />
</Columns>
</asp:GridView>
And my GetLink function:
protected void GetLink(object sender, EventArgs e)
{
// start off empty
string DetailIDList = "";
foreach (GridViewRow row in DieListDetailGridView.Rows) {
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkBox = (row.Cells[0].FindControl("chkBox") as CheckBox);
string DetailID = row.Cells[3].Text;
if (chkBox.Checked)
{
DetailIDList = DetailIDList + DetailID + ",";
}
}
}
// Remove the last , from the list
DetailIDList = DetailIDList.Remove(DetailIDList.Length - 1, 1);
// comma delimeted list of checked Detail ID's
lblTesting.Text = DetailIDList;
//Response.Redirect("~/Tooling/DieDetail.aspx?DetailIDList=");
}
I can't figure out why my DetailID is empty.
I've tried doing row.Cells[0].FindControl("DetailID") but that also didn't work.
What I am expecting to get from the DetailID are integers like "28925", "16423" etc.
I was asked to not show the column on the page, which is why visible = false. I still need to reference the ID to pass it to the edit page. I need to do the same edit to multiple Detail ID's.
What am I doing wrong?
The answer to having a list of ID's even though the datafield is set to not be visible was found on another StackOverflow question: ASP.NET: GridView value set to null when column is not visible
The answer was to remove the "Visible=false" from the GridView and on each row created (after the column has the data added for the row) make it not visible.
This code fixed my issue:
protected void DieListDetailGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
}
I am building a gridview from an arraylist and want to include a footer to the bottom.
This is my c# code
gvOrder.DataSource = orderItemList;
gvOrder.DataBind();
gvOrder.ShowFooter = true;
gvOrder.Columns[0].FooterText = "Totals:";
gvOrder.Columns[2].FooterText = Convert.ToString(quantity);
gvOrder.Columns[4].FooterText = Convert.ToString(priceTotal);
Here is my asp code
<asp:GridView ID="gvOrder" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:BoundField DataField="ItemTitle" HeaderText="Title" />
<asp:BoundField DataField="ItemFormat" HeaderText="Format" />
<asp:BoundField DataField="ItemQuantity" HeaderText="Quantity" />
<asp:BoundField DataField="ItemPrice" HeaderText="Price" />
<asp:BoundField DataField="ItemTotal" HeaderText="Total" />
</Columns>
</asp:GridView>
The order of things matter. You have to set the footer values before DataBind() is called.
gvOrder.Columns[0].FooterText = "Totals:";
gvOrder.Columns[2].FooterText = Convert.ToString(quantity);
gvOrder.Columns[4].FooterText = Convert.ToString(priceTotal);
gvOrder.DataSource = orderItemList;
gvOrder.DataBind();
gvOrder.ShowFooter = true;
But you can set the values of the Footer Row after DataBind if you specify the footerrow cells instead of the Colums.
gvOrder.FooterRow.Cells[1].Text = "After DataBind";
I have a gridview in my ASP.NET. in my gridview columns, i want the button on each row to show or hide if the value of a cell is the same cell is empty or null. for example, i want a button to show on each row that has Signout_Time as null or empty. i have written the code below. the issue i'm having is that the codes works in an opposite way. Buttons are showed on the rows with "Signout_Time" while the button on rows without a value for "Signout_Time" visibility becomes false. it shouldn't be so. I have also tried changing my if conditions, it still didnt work
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
switch (e.Row.RowType) {
case DataControlRowType.DataRow:
DataRowView myDataRowView = (DataRowView)e.Row.DataItem;
if (String.IsNullOrEmpty(myDataRowView["Signout_Time"].ToString())) {
Button status = (Button)e.Row.FindControl("out");
if (status != null) {
status.Visible = true;
}
}
break
}
}
<Columns>
<asp:BoundField HeaderText="S/N" DataField="SN" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
<asp:BoundField HeaderText="Sex" DataField="Sex" />
<asp:BoundField HeaderText="Reason" DataField="Reason" />
<asp:BoundField HeaderText="SignIn" DataField="SignIn_Time" />
<asp:BoundField HeaderText="SignOut" DataField="Signout_Time" />
<asp:TemplateField HeaderText="Action" Visible="True">
<ItemTemplate>
<asp:Button ID="out" runat="server" Text="Sign out" CommandName="SignOut" CommandArgument='<%#Eval("SN") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" />
</asp:GridView>
You can set the button Visibility directly in the GridView Template:
<asp:Button Visible='<%# string.IsNullOrEmpty(Eval("Signout_Time").ToString()) %>' runat="server" Text="Sign out" ID="out" CommandName="SignOut" CommandArgument='<%#Eval("SN") %>'/>
I want to access the HyperLinkField column of the GridView from code behind but I am unable to do so.
I have the column in my dataset but I am still not able to use it.
My HyperLinkField column is named Status.
Here is what I tried:
UltraWebGrid1.DataSource = ObjPriDsGrid;
UltraWebGrid1.DataBind();
string StrPriStatus = "";
for (int IntPriI = 0; IntPriI < UltraWebGrid1.Rows.Count; IntPriI++)
{
if (UltraWebGrid1.Rows[IntPriI].Cells[6].Text.Trim() != null)
{
StrPriStatus = UltraWebGrid1.Rows[IntPriI].Cells[6].Text.Trim();
}
else
{
}
if (StrPriStatus == "5")
{
UltraWebGrid1.Rows[IntPriI].Cells[8].Text = ""; // Not getting status column here
}
}
Here is my GridView:
<asp:GridView ID="UltraWebGrid1" ShowHeader="true" runat="server" AutoGenerateColumns="false"
DataKeyNames="mkey" OnRowDataBound="Grid_RowDataBound" Width="98%" Height="30%"
PageSize="10" AllowPaging="true" OnPageIndexChanging="Grid1_PageIndexChanging"
ShowFooter="true" CssClass="Grid">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="../../../Images/plus.png" />
<asp:Panel ID="pnlGrid" runat="server" Style="display: none">
<asp:GridView ID="Grid2" runat="server" AutoGenerateColumns="false" Width="600px"
CssClass="ChildGrid">
<Columns>
<asp:BoundField ItemStyle-Width="80px" DataField="RefMkey" HeaderText="Mkey" />
<asp:BoundField ItemStyle-Width="150px" DataField="CurrentUser" HeaderText="Current User" />
<asp:BoundField ItemStyle-Width="180px" DataField="Department" HeaderText="Current Department" />
<asp:BoundField ItemStyle-Width="100px" DataField="remarks" HeaderText="Remarks" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="0px" DataField="mkey" Visible="false" HeaderText="Mkey" />
<asp:BoundField ItemStyle-Width="8%" DataField="Doc_No" HeaderText="IW/ No" />
<asp:BoundField ItemStyle-Width="10%" DataField="Doc_Date" HeaderText="IW/ Date" />
<asp:BoundField ItemStyle-Width="12%" DataField="DocType" HeaderText="Doc type" />
<asp:BoundField ItemStyle-Width="15%" DataField="Party_Name" HeaderText="Party Name" />
<asp:BoundField ItemStyle-Width="0" Visible="true" DataField="Status_Flag" HeaderText="Status" />
<asp:BoundField ItemStyle-Width="10%" DataField="LastAction_datetime" HeaderText="Last Action Date" />
<asp:BoundField ItemStyle-Width="10%" DataField="CurrStatus" HeaderText="Current Status" />
<asp:BoundField ItemStyle-Width="10%" DataField="Type_desc" HeaderText="Resp Dept" />
<asp:BoundField ItemStyle-Width="12%" DataField="UserName" HeaderText="Resp User" />
<asp:BoundField ItemStyle-Width="8%" DataField="No_Of_Days" HeaderText="No of days" />
<asp:HyperLinkField ItemStyle-Width="5%" DataNavigateUrlFields="Mkey, Status, Doc_No"
DataNavigateUrlFormatString="~/Administration/Dispatch/Inward/FrmInwardNextAction.aspx?Inward_mkey={0}&Status={0}&IWNO={0}"
HeaderText="Status" DataTextField="Status" Target="_blank" />
<asp:HyperLinkField ItemStyle-Width="5%" DataNavigateUrlFields="Mkey, Status, Doc_No"
HeaderText="View" DataTextField="View" Target="_blank" DataNavigateUrlFormatString="~/Administration/Dispatch/Inward/InwardDocDetails.aspx?Key={0}&Status={0}&IWNO={0}" />
</Columns>
</asp:GridView>
Columns like HyperLinkField and CheckBoxField behave a little differently than BoundField. They don't just simply contain text like BoundField. You have to get the control within the cell. You can get the child controls of the cell using the Controls collection. We know that specifically for a HyperLinkField, the HyperLink will be the first control in the collection.
HyperLink hyp = (HyperLink)UltraWebGrid1.Rows[IntPriI].Cells[12].Controls[0];
Can you try it like this
if (StrPriStatus == "5")
{
HyperLink HyperLinkObj = (HyperLink)UltraWebGrid1.Rows[IntPriI].Cells[12].Controls[0];
HyperLinkObj.Text = "";
}
I have a gridview with a template field. In that template field is a checkbox. I have a submit button outside of the gridview to assign the records that were checked. On the postback no checkboxes register as being checked. Here is my Code:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb" Checked="false" runat="server" />
<asp:Label ID="lblCFID" runat="server" Visible="false" Text='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="Name" HeaderText="Name" />
<asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="DOB" HeaderText="Date of Birth" />
<asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Gender" DataField="Gender" />
<asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Status" DataField="Status" />
<asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Plan Name" DataField="PlanName" />
<asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Type" DataField="ControlType" />
<asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Date of Service" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" DataField="DateofService" />
</Columns>
protected void AssignRecords(object sender, EventArgs e)
{
int Rows = gvASH.Rows.Count;
for (int i = 0; i < Rows; i++)
{
//CheckBoxField cb = ((CheckBoxField)gvASH.Rows[i].Cells[1]).;
CheckBox cb = (CheckBox)gvASH.Rows[i].Cells[0].FindControl("cb");
Label lblID = (Label)gvASH.Rows[i].Cells[0].FindControl("lblCFID");
if (cb.Checked == true)
{
string ID = lblID.Text;
//Assign Code
}
}
}
I have a breakpoint set on the string ID = lblID.Text; but it never finds any that are checked.
I think what you are missing is, when you click on the button and your page is postback, you rebinding to gridview, you need to bind in this condition like
if (!Page.IsPostBack)
{
GridView1.DataSourceID = "yourDatasourceID";
GridView1.DataBind();
}
On a postback, the contents of the GridView are re-created from the postback Viewstate data between page_init and page_load. Perhaps try examining your Gridview in page_load to see what's there.
set the autopostback attribute of Checkbox
AutoPostBack="true"