Tere is four level of gridview (nested), everything is working fine but for "gvEmployees"
I Have Access "gvDepartments", "gvFuctions" & "gvSections"
How can i access the "gvEmployees"?
<asp:GridView ID="gvDepartments" runat="server" AutoGenerateColumns="false" CssClass="Grid" Width="100%" DataKeyNames="EMPLOYEE_NUMBER">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgFunctionShow" runat="server" OnClick="Show_Hide_FunctionsGrid" ImageUrl="~/images/plus.png" CommandArgument="Show" />
<asp:Panel ID="pnlFunctions" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvFunctions" runat="server" AutoGenerateColumns="false" PageSize="10" Width="100%" AllowPaging="true"
OnPageIndexChanging="OnFunctionsGrid_PageIndexChanging" CssClass="ChildGrid" DataKeyNames="EMPLOYEE_NUMBER">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgSectionsShow" runat="server" OnClick="Show_Hide_SectionsGrid" ImageUrl="~/images/plus.png" CommandArgument="Show" />
<asp:Panel ID="pnlSections" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvSections" runat="server" AutoGenerateColumns="false" PageSize="10" Width="100%"
AllowPaging="true" OnPageIndexChanging="OnSectionsGrid_PageIndexChanging">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgEmployeesShow" runat="server" OnClick="Show_Hide_EmployeesGrid" ImageUrl="~/images/plus.png" CommandArgument="Show" />
<asp:Panel ID="pnlEmployees" runat="server" Visible="false" Style="position: relative">
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false" PageSize="10" Width="100%"
AllowPaging="true" OnPageIndexChanging="OnEmployeesGrid_PageIndexChanging" >
<Columns>
<asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
<asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
<asp:BoundField DataField="Position" HeaderText="Position" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
<asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
<asp:BoundField DataField="Section" HeaderText="Section" />
<asp:BoundField DataField="Position" HeaderText="Position" />
<asp:BoundField DataField="SVName" HeaderText="Reporting To" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
<asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
<asp:BoundField DataField="FunctionName" HeaderText="Function Name" />
<asp:BoundField DataField="Position" HeaderText="Position" />
<asp:BoundField DataField="SVName" HeaderText="Reporting To" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EMPLOYEE_NUMBER" HeaderText="Employee#" />
<asp:BoundField DataField="EMPNAMEE" HeaderText="Employee Name" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="Position" HeaderText="Position" />
<asp:BoundField DataField="LocInfo" HeaderText="Location" />
<asp:BoundField DataField="SVName" HeaderText="Reporting To" />
</Columns>
</asp:GridView>
C# Code:
protected void Show_Hide_EmployeesGrid(object sender, EventArgs e)
{
ImageButton imgShowHide = (sender as ImageButton);
GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
if (imgShowHide.CommandArgument == "Show")
{
row.FindControl("pnlEmployees").Visible = true;
imgShowHide.CommandArgument = "Hide";
imgShowHide.ImageUrl = "~/images/minus.png";
string empId = (row.NamingContainer as GridView).DataKeys[row.RowIndex].Value.ToString();
GridView gvEmployees = row.FindControl("gvEmployees") as GridView;
BindEmployees(empId, gvEmployees);
//gvEmployees.
}
else
{
row.FindControl("pnlEmployees").Visible = false;
imgShowHide.CommandArgument = "Show";
imgShowHide.ImageUrl = "~/images/plus.png";
}
}
private void BindEmployees(string empId, GridView gvEmployees)
{
gvEmployees.ToolTip = empId.ToString();
gvEmployees.DataSource = GetData(string.Format("SELECT empinfo_v_update.EMPLOYEE_NUMBER, empinfo_v_update.EMPNAMEE, empinfo_v_update.POSITION, empinfo_v_update.FunctionName, " +
" empinfo_v_update.UserLevel, UserLevel.Description, empinfo_v_update.Section, 0 AS EmployeeLevel, " +
" empinfo_v_update_1.EMPNAMEE AS SVName " +
" FROM empinfo_v_update INNER JOIN " +
" UserLevel ON empinfo_v_update.UserLevel = UserLevel.UserLevel INNER JOIN " +
" empinfo_v_update AS empinfo_v_update_1 ON " +
" empinfo_v_update.SupervisorID = empinfo_v_update_1.EMPLOYEE_NUMBER " +
" WHERE (empinfo_v_update.SupervisorID ='{0}') ORDER BY LEN(empinfo_v_update.EMPLOYEE_NUMBER),empinfo_v_update.EMPLOYEE_NUMBER", empId));
gvEmployees.DataBind();
}
protected void OnEmployeesGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvEmployees = (sender as GridView);
gvEmployees.PageIndex = e.NewPageIndex;
BindEmployees(gvEmployees.ToolTip, gvEmployees);
}
I tried the Code in C# as writted below:
There is a simple example, it works well for me,
Related
This code return the error "System.ArgumentOutOfRangeException". I know this error but my gridView have 13 columns and I read the column 2 and 4 so I don't understand...
Only e.Row.Cells[0] works so I can't read the other columns.
I looked on the forum but I can not find my problem. Often this problem is due to the fact that the person tries to read a column that does not exist, this is not my case.
protected void grd_accident_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Check CM
if (e.Row.Cells[1].Text.Equals("false"))
{
e.Row.Cells[1].Text = "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>";
}
else
{
e.Row.Cells[1].Text = "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span>";
}
////Check HSE
if (e.Row.Cells[3].Text.Equals("2"))
{
e.Row.Cells[3].Text = "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>";
}
else
{
e.Row.Cells[3].Text = "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span>";
}
}
If it can help, my gridView :
<asp:GridView ID="grd_accident" runat="server" CellPadding="4" AutoGenerateColumns="False"
AllowPaging="True"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" DataSourceID="ATP_rechercheRes" OnRowDataBound="grd_accident_RowDataBound" OnRowCommand="grd_accident_RowCommand">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="columna" HeaderText="Type" SortExpression="columna" />
<asp:BoundField DataField="columnb" HeaderText="ID" ReadOnly="True" SortExpression="columnb" />
<asp:BoundField DataField="columnc" ItemStyle-Width="90px" HeaderText="Date" SortExpression="columnc" />
<asp:BoundField DataField="columnd" HeaderText="Heure" SortExpression="columnd" />
<asp:BoundField DataField="columne" HeaderText="Nom" SortExpression="columne" />
<asp:BoundField DataField="columnf" HeaderText="Prénom" SortExpression="columnf" />
<asp:BoundField DataField="columng" HeaderText="Badge" SortExpression="columng" />
<asp:BoundField DataField="columnh" HeaderText="Société" SortExpression="columnh" />
<asp:BoundField DataField="columni" HeaderText="Description" SortExpression="columni" />
<asp:BoundField DataField="columnj" ItemStyle-HorizontalAlign="Center" HeaderText="Code EPI" SortExpression="columnj" />
<asp:BoundField DataField="columnk" ItemStyle-HorizontalAlign="Center" HeaderText="Advisor" SortExpression="columnk" />
<asp:BoundField DataField="columnl" ItemStyle-HorizontalAlign="Center" HeaderText="HSE" SortExpression="columnl" />
<asp:TemplateField ItemStyle-Width="130px">
<ItemTemplate>
<asp:ImageButton ID="btn_goToAccidentInfirmiere" ImageUrl="images/infirmerie.png" Width="37px" runat="server"
CommandName="goToAccidentInfirmiere"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Infirmière ►" />
<asp:ImageButton ID="btn_goToAccidentCm" ImageUrl="images/factory.png" Width="37px" runat="server"
CommandName="goToAccidentCm"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="CM ►" />
<asp:ImageButton ID="btn_genRapport" ImageUrl="images/rapport.png" Width="37px" runat="server" CommandName="genRap"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
</asp:GridView>
I found the solution, I removed AllowPaging="True" on from the properties of GridView. It works now
i have a button in my gridview with the ID "btnApprove". what i want is when the user clicks the button, the row "Status" will be updated to 'Approved'. how can i acheive that? one row will only be updated when the button is clicked depending on the transaction number
here is my aspx code.
<asp:UpdatePanel ID="panel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TransactionID" OnRowDataBound="GridView1_OnRowDataBound" OnRowCommand="GridView1_RowCommand" CellPadding="4" AllowPaging="true" PageIndex="2" OnPageIndexChanging="GridView1_PageIndexChanging" HeaderStyle-BackColor ="CornflowerBlue" BorderWidth="1" BorderColor="Gray" Width="100%" CssClass=" table table-hover" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<img style="cursor:pointer" src ="../Images/Icons/plus2.png" />
<asp:Panel ID ="pnlDetails" runat="server" Style="display: none">
<asp:GridView ID="gvDet" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<Columns>
<%--<asp:BoundField ItemStyle-Width="20px" DataField="ID" HeaderText="ID" />--%>
<asp:BoundField ItemStyle-Width="200px" DataField="ItemType" HeaderText="Type" />
<asp:BoundField ItemStyle-Width="250px" DataField="ItemModel" HeaderText="Model" />
<asp:BoundField ItemStyle-Width="140px" DataField="ItemQuantity" HeaderText="Requested Quantity" />
<asp:BoundField ItemStyle-Width="80px" DataField="ItemUnit" HeaderText="Unit" />
<asp:BoundField ItemStyle-Width="100px" DataField="ItemDate" HeaderText="Date Needed" />
<asp:BoundField ItemStyle-Width="200px" DataField="ItemDesc" HeaderText="Description" />
<%--<asp:BoundField ItemStyle-Width="80px" DataField="ItemStatus" HeaderText="Status" />--%>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="TransactionID" HeaderText="Transaction Number" />
<asp:BoundField ItemStyle-Width="150px" DataField="DateFiled" HeaderText ="Date Filed" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqName" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqCompany" HeaderText="Company" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqBranch" HeaderText="Branch" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqBU" HeaderText="Business Unit" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqDept" HeaderText="Department" />
<asp:BoundField ItemStyle-Width="150px" DataField="ReqSection" HeaderText="Section" />
<asp:BoundField ItemStyle-Width="150px" DataField="TransStatus" HeaderText="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click" CssClass="btn btn-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="CornflowerBlue" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
Update here is my data source of my GridView
public void showTable()
{
Utility u = new Utility();
string conn = u.connect();
SqlConnection connUser = new SqlConnection(conn);
SqlDataAdapter adp = new SqlDataAdapter("select * from MosefTransaction where TransStatus = 'Pending'", connUser);
DataTable dt = new DataTable();
connUser.Open();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Add CommandArgument='<%# Container.DataItemIndex %>' to your button and in your code behind you can get the row that raised the event on Gridview's RowCommand event
<asp:Button ID="btnApprove" runat="server" Text="Approve" CommandName="ApproveTransaction" CommandArgument='<%# Container.DataItemIndex %>'/>
In your codebehind subscribe to gridview's row command event.
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ApproveTransaction")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvInfo.Rows[index];
string cellText = row.Cells[2].Text;
//Update your data in database here and rebind the gridview to updated data
}
}
MY grid view code
<asp:GridView ID="grdAccidentMaster" runat="server" AutoGenerateColumns="False"
Width="80%" BackColor="White" BorderColor="#DEDFDE" BorderStyle="Solid" BorderWidth="1px"
EmptyDataText="No Records found" CellPadding="4" ForeColor="Black" GridLines="Both"
DataKeyNames="IncidentNo" OnRowDataBound="grdAccidentMaster_RowDataBound">
<Columns>
<asp:BoundField DataField="IncidentNo" HeaderText="IncidentNo" />
<asp:BoundField DataField="CreatedDate" HeaderText="Created Date" />
<asp:BoundField DataField="AccidentDate" HeaderText="Accident Date" />
<asp:BoundField DataField="PoliceStation" HeaderText="Police Station" />
<asp:BoundField DataField="City" HeaderText="City Name" />
<asp:BoundField DataField="RoadCondition" HeaderText="Road Condition" />
<asp:BoundField DataField="RoadFeature" HeaderText="Road Feature" />
<asp:TemplateField HeaderText="Print">
<ItemTemplate>
<asp:Button runat="server" Text="Click" ID="btnClick" OnClick="btnPrint_click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
MY grid view Output
If i click the Click button under Print column. I need to get the IncidentNo value '4022' on onclick function.
Add an event in Gridview :
OnRowCommand="grdAccidentMaster_OnRowCommand"
Change template for button :
<ItemTemplate>
<asp:Button runat="server" Text="Click" ID="btnClick"
CommandName="SendIncidentNo" CommandArgument='<%# Eval("IncidentNo") %>' />
</ItemTemplate>
Code behind :
protected void grdAccidentMaster_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "SendIncidentNo") return;
int incidentNo = Convert.ToInt32(e.CommandArgument);
// do something
}
I have a GridView that binds values from the database.
Here is the code:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Title" />
<asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
</asp:ImageField>
<asp:BoundField DataField="ProductDescription" HeaderText="Description" />
<asp:BoundField DataField="ProductCost" HeaderText="Cost" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" TextMode="Number" ID="txtQuantity"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Button" CommandName="AddToCart" />
</Columns>
</asp:GridView>
I want to pass the command arguments containing values from txtQuantity and ProductID to the code behind on CommandName "AddToCart".
How can I do this?
Bind the values of the ProductID & Quantity in the GridView. In GridView, give like this:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="DataView" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Title" />
<asp:ImageField DataImageUrlField="ProductId" DataImageUrlFormatString="getProductImage.ashx?ProductID={0}" HeaderText="Image">
</asp:ImageField>
<asp:BoundField DataField="ProductDescription" HeaderText="Description" />
<asp:BoundField DataField="ProductCost" HeaderText="Cost" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" TextMode="Number" ID="txtQuantity" Text="<%# Bind("Quantity")%>"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:Button Text="Button" CommandName="AddToCart" CommandArgument="<%# Eval("Quantity") + "," + Eval("ProductID")%>" />
</Columns>
</asp:GridView>
In GridView RowCommand event, give the below code:
if(e.CommandName == "AddToCart")
{
string[] args = e.CommandArgument.ToString().Split(",");
Decimal Quantity = Convert.ToDecimal(args[0]);
int ProductID = Convert.ToInt32(args[1]);
}
i'm using a GridView and trying to sort and page it.
I have more than 20 rows on my DataBase, but the GridView just shows 10.
I set AllowPaging=true but noting happened.
Also I'm using AllowSorting = true and OnSorting="GridView_Sorting". But when I click on the header to sort the the content of that column, it does not enters on my OnSorting="GridView_Sorting" it goes right to the GridView1_RowCommand why ?
Sometimes it just give me this error:
Object reference not set to an instance of an object error
Here's my Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="font-family: Verdana, Arial, Sans-Serif;"
CssClass="gridview" OnSorting="GridView_Sorting"
AllowSorting ="True" AllowPaging="True" BackColor="#CCCCCC"
BorderStyle="Inset" BorderWidth="2px" BorderColor="GrayText"
CellPadding="1"
CellSpacing="5"
HeaderStyle-HorizontalAlign="Center"
OnRowDataBound="GridView1_RowDataBound"
ForeColor = "Black" RowStyle-CssClass="gridview"
OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="#CCCCCC" />
<columns>
<asp:BoundField HeaderText="ID" DataField="id" SortExpression="id" />
<asp:BoundField HeaderText="PRIORIDADE" DataField="prioridade" ItemStyle-HorizontalAlign="Center" SortExpression="prioridade" SortExpression="prioridade" />
<asp:BoundField HeaderText="SITUAÇÃO" DataField="situacao" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="RESPONSAVEL" DataField="responsavel" HeaderStyle-Width="65px" ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="65px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="DATA DE CADASTRO" DataField="dt_cadastro" SortExpression="dt_cadastro" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"ItemStyleHorizontalAlign="Center" >
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="PREVISÃO DE TÉRMINO" DataField="previsao_termino" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"
ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="PROJETO" DataField="projeto" ItemStyle-HorizontalAlign="Center"></asp:BoundField>
<asp:BoundField HeaderText="FUNCIONALIDADE" DataField="funcionalidade" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="CLUBE" DataField="clube" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderStyle-Width="70px" HeaderText="VISUALIZAR" >
<ItemTemplate>
<asp:Button ID="Btn_Visualizar" runat="server" Text="VISUALIZAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial" OnClick="Btn_Visualizar_Click"CommandName="visualizar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="66px" HeaderText="ALTERAR">
<ItemTemplate>
<asp:Button ID="Btn_Alterar" runat="server" Text="ALTERAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial"CommandName="editar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="66px" HeaderText="FEEDBACK">
<ItemTemplate>
<asp:Button ID="Btn_Feedback" runat="server" Text="ADICIONAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana,Arial"CommandName="feedback" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</columns>
My CodeBehind:
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
string[] strSortExpression = ViewState["SortExpression"].ToString().Split(' ');
// If the sorting column is the same as the previous one,
// then change the sort order.
if (strSortExpression[0] == e.SortExpression)
{
if (strSortExpression[1] == "ASC")
{
ViewState["SortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
}
}
// If sorting column is another column,
// then specify the sort order to "Ascending".
else
{
ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
}
// Rebind the GridView control to show sorted data.
GridView1.DataSource = ch.BuscaTodosChamados();
GridView1.DataBind();
}
Add SortExpression attribute for each sortable column, informing its DataField.