i am doing to this with my gridview in aspx file
<asp:GridView ID="gridDepartement" runat="server" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" AllowSorting="True" >
<Columns>
<asp:templatefield>
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll"
runat="server" AutoPostBack="true"
OnCheckedChanged="cbSelectAll_CheckedChanged" />
</HeaderTemplate>
<itemtemplate>
<asp:CheckBox Id="cbSelectOne" runat="server"/>
</itemtemplate>
</asp:templatefield>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="20"/>
<asp:CommandField ShowDeleteButton="True" ItemStyle-Width="20"/>
<asp:CommandField ShowSelectButton="True" ItemStyle-Width="20"/>
<asp:boundfield headertext="Departement Code" datafield="departementcode"
ItemStyle-HorizontalAlign="Center"/>
<asp:boundfield headertext="Departement Name" datafield="departementname"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Created By" datafield="createby"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Created Date" datafield="createdate"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Updated By Name" datafield="updateby"
ItemStyle-HorizontalAlign="Center" />
<asp:boundfield headertext="Last Update" datafield="lastupdate"
ItemStyle-HorizontalAlign="Center" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
and i want check all checkbox when checkbox header clicked, so i add an event to my grid, and add this code in checkbox checkchanged event :
protected void cbSelectAll_CheckedChanged(object sender, EventArgs e)
{
bool chkFlag = false;
CheckBox cbHD = (CheckBox)gridDepartement.HeaderRow.FindControl("cbSelectAll");
if (cbHD.Checked)
{
chkFlag = true;
}
foreach (GridViewRow dr in gridDepartement.Rows)
{
CheckBox chk = (CheckBox)dr.Cells[0].FindControl("cbSelectOne");
chk.Checked = chkFlag;
}
}
page load code :
protected void Page_Load(object sender, EventArgs e)
{
//if(!IsPostBack)
//{
DataSourceDepartement dpt = new DataSourceDepartement();
DataSourceDepartementTableAdapters.departementTableAdapter
adp = new DataSourceDepartementTableAdapters.departementTableAdapter();
//bind gridview to datatable
gridDepartement.DataSource = adp.GetDataDepartement();
gridDepartement.DataBind();
//}
}
ok it's working now, but now the problem is, my checkbox event only trigered when it is value turn to checked /true, but when i uncheck it / turn it to false, it does not trigered, what part i should fix?
Why to do this thing in server side. Do it in java script. That will be better for performance level.Use this code::: `function CheckAll(objparentcheckbox) {
var HeaderCheckboxControl = objparentcheckbox
var table = getParentByTagName(HeaderCheckboxControl, 'table');
//get all the control of the type INPUT in the base control.
var Inputs = table.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox') {
Inputs[n].checked = HeaderCheckboxControl.checked;
}
return false;
} and in grid:::: <HeaderTemplate>
<asp:CheckBox ID="ChkSelectAll" onclick="CheckAll(this)" runat="server" />
</HeaderTemplate>`
Your Gridview is binded again before the event of checkbox, thats why your event is not called instead of postback so put your gridview binding statement in following block.
if (!Page.IsPostBack)
{
gridDepartement.DataSource = adp.GetDataDepartement();
gridDepartement.DataBind();
}
function SelectAll(objcheckbox)
{
var HeaderCBControl = objcheckbox;
//var table = getParentByTagName(HeaderCBControl, 'table');
var Inputs = document.getElementById('CenterContent_gridDepartement').getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox')
{
Inputs[n].checked = HeaderCBControl.checked;
}
return false;
}
</script>
Related
I'd like to send gridview rows to an email on button press. The number of rows selected vary and use check boxes.
Background:
The app consists of 2 GridViews. One acts as a schedule, the other acts as a holding area for items taken out of the schedule. Included below is what I use to move Items out of the schedule. The code to put items back into schedule is identical, so it was not included.
The email function is to notify the user what specific items were taken out/put into schedule.
GridView1
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID" AllowPaging="True" ShowHeaderWhenEmpty="True" BackColor="White" BorderColor="#003399" BorderStyle="None" BorderWidth="1px" CellPadding="4" OnPageIndexChanging="GridView1_PageIndexChanging" Width="450px">
<Columns>
<asp:BoundField DataField="VEH_SER_NO" SortExpression="VEH_SER_NO" ReadOnly="True">
<ItemStyle HorizontalAlign="Center" Width="75px" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="DATE_BLD_RATE" SortExpression="DATE_BLD_RATE" ReadOnly="True" DataFormatString="{0:yyyy/MM/dd}">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="SEQ" SortExpression="SEQ" ReadOnly="True">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="SV_TYPE_CD" SortExpression="SV_TYPE_CD" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="COMMENT" SortExpression="COMMENT" Visible="False" ReadOnly="False" />
<asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkSel" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#99CCCC" ForeColor="White" HorizontalAlign="center" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
Code:
protected void cmdMoveRight_Click(object sender, EventArgs e)
{
if (drpReason0.SelectedValue != "" && drpReason.SelectedValue != "")
{
string strSQL = "SELECT VEH_SER_NO, DATE_BLD_RATE, SEQ, SV_TYPE_CD, COMMENT FROM Schedule " +
"WHERE Schedule.id = #ID";
MoveRows(GridView1, strSQL);
}
}
void MoveRows(GridView gv, string strSQL)
{
foreach (GridViewRow OneRow in gv.Rows)
{
CheckBox ckBox = OneRow.FindControl("cHkSel") as CheckBox;
if (ckBox.Checked)
{
int PKID = (int)gv.DataKeys[OneRow.RowIndex]["ID"];
SqlCommand cmdSQL = new SqlCommand(strSQL);
cmdSQL.Parameters.Add("#ID", SqlDbType.Int).Value = PKID;
SqlRun(cmdSQL);
}
}
LoadGrids();
}
public void SqlRun(SqlCommand cmdSQL)
{
using (SqlConnection conn = new SqlConnection(cs))
{
using (cmdSQL)
{
cmdSQL.Connection = conn;
conn.Open();
cmdSQL.ExecuteNonQuery();
}
}
}
I'm trying to enable paging for my gridview, however I'm always getting error:
"Specified argument was out of the range of valid values. Parameter name: index"
If i ignore the error the gridview creates the paging and works fine, but there is an error ignored...
This is my grid:
<asp:GridView ID="gvDeslocFinal" runat="server" Height="181px" Width="1042px" OnRowDataBound="gvDeslocFinal_RowDataBound" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gvDeslocFinal_Sorting" OnPageIndexChanging="gvDeslocFinal_PageIndexChanging" OnSorted="gvDeslocFinal_Sorted" AllowPaging="True" OnRowCreated="gvDeslocFinal_RowCreated" PageIndex="1">
<Columns>
<asp:BoundField DataField="Utilizador" HeaderText="Utilizador" />
<asp:BoundField DataField="Carro" HeaderText="Carro" />...
<ItemStyle CssClass="hidden-field" />
</asp:BoundField>
<asp:TemplateField HeaderText="Detalhes">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" Text="ver detalhes" OnClick="lnkEdit_Click" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle HorizontalAlign="Center" />
<HeaderStyle VerticalAlign="Middle" HorizontalAlign="Center" Font-Bold="False" Height="30px" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="20px" />
</asp:GridView>
protected void gvDeslocFinal_Sorted(object sender, EventArgs e)
{
if (gvDeslocFinal.EditIndex >= 0)
return;
BindGrid();
}
BindGrid function:
private void BindGrid()
{
try
{
for (int i = 0; i < GlobalOffline.DesTerm.Length; i++)
{
string sDataIn = smtgh;
...
string sCodigo = GlobalOffline.DesTerm[i].Codigo;
string[] sRow = new string[] { sCarro, ..., sCodigo };
GlobalOffline.dtResult.Rows.Add(sRow);
}
gvDeslocFinal.DataSource = GlobalOffline.dtResult;
gvDeslocFinal.VirtualItemCount = GlobalOffline.dtResult.Rows.Count;
GlobalOffline.bDetails = false;
gvDeslocFinal.DataBind();
}
In my gridview, there is a column IsYearly. The value can be 1 or 0.
On page load, the default values of all the data is 0, but when I update a data, the gridview will become what it is supposed to be on page load.
Here is the gridview:
<asp:GridView ID="gridView_Holidays" runat="server" AutoGenerateColumns="False" Style="margin-right: 10px; margin-top: 0px;" AllowPaging="True" OnPageIndexChanging="grdHolidays_PageIndexChanging" OnRowDataBound="grdHolidays_RowDataBound" PageSize="10" CssClass="table-grid">
<Columns>
<asp:BoundField DataField="HolidayName" HeaderText="Name">
<HeaderStyle BackColor="#0078D7" ForeColor="White" Width="320px" />
<ItemStyle BackColor="White" ForeColor="Black" Width="320px" />
</asp:BoundField>
<asp:BoundField DataField="HolidayDate" HeaderText="Date">
<HeaderStyle BackColor="#0078D7" ForeColor="White" Width="256px" />
<ItemStyle BackColor="White" ForeColor="Black" Width="256px" />
</asp:BoundField>
<asp:BoundField DataField="HolidayID" HeaderText="Holiday ID">
<HeaderStyle BackColor="#0078D7" ForeColor="White" CssClass="HideControl" />
<ItemStyle BackColor="White" ForeColor="Black" CssClass="HideControl" />
</asp:BoundField>
<asp:BoundField DataField="HolidayIsYearly" HeaderText="Holiday Yearly">
<HeaderStyle BackColor="#0078D7" ForeColor="White" CssClass="HideControl" />
<ItemStyle BackColor="White" ForeColor="Black" CssClass="HideControl" />
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="40px" HeaderText="" ItemStyle-CssClass="tblItem" HeaderStyle-CssClass="header">
<ItemTemplate>
<asp:ImageButton ID="imgBtnEditHoliday" runat="server" OnClick="lnkEditHoliday_Click" ImageUrl="~/Images/EDIT.GIF" ToolTip="Edit" />
<asp:ImageButton ID="imgBtnDeleteHoliday" runat="server" Width="16px" OnClick="lnkDeleteHoliday_Click" ImageUrl="~/Images/deleteuser.png" ToolTip="Delete" />
</ItemTemplate>
<HeaderStyle BackColor="#0078D7" ForeColor="White" Font-Size="Medium" />
<ItemStyle BackColor="White" ForeColor="Black" Font-Size="Medium" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No results to show.
</EmptyDataTemplate>
</asp:GridView>
The code behind:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
HolidayList();
imgBtnSearchApprover_Click(this, EventArgs.Empty);
}
}
public void HolidayList() {
Admin = new List<DTR_Admin>();
Admin = mServiceHoliday.GetList();
gridView_Holidays.DataSource = Admin;
gridView_Holidays.DataBind();
}
This is what happens when I click save button, where the gridview (i think) will refresh so it shows the correct data
protected void btnHolidaySave_Click(object sender, ImageClickEventArgs e) {
if (string.IsNullOrEmpty(txtHolidayName.Text) || string.IsNullOrEmpty(txtHolidayDate.Text)) {
lblCostCenterError.Text = "Please complete all fields.";
ModalPopupExtender2.Show();
}
else {
if (Admin.Exists(x => (Convert.ToDateTime(x.HolidayDate) == Convert.ToDateTime(txtHolidayDate.Text) && x.HolidayID != holidayID))) {
ClientScript.RegisterStartupScript(GetType(), "load", "alert('Date already exists.');", true);
ModalPopupExtender2.Show();
}
else {
DTR_HolidayService holidayService = new DTR_HolidayService();
DTR_Admin pEntity = new DTR_Admin();
pEntity.HolidayID = holidayID;
pEntity.HolidayDate = txtHolidayDate.Text;
pEntity.HolidayName = txtHolidayName.Text;
pEntity.HolidayIsYearly = cbYearly.Checked ? 1 : 0;
holidayService.EditHoliday(pEntity);
HolidayList();
}
}
}
I think that you can use this code for setting the default in the code for the database
public int _HolidayIsYearly= 1;
public int HolidayIsYearly
{
get { return _HolidayIsYearly; }
set { _HolidayIsYearly= value; }
}
public byte HolidayIsYearly { get; set; }
please check this solution
I am trying to get the selected row data and transfer it to a label on the same page. However I cannot get the Gridview.SelectedRow to work with my CommandName. I have tried everything....
I get the error. Object reference not set to an instance of an object. on Label2.Text
Here is my Code:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Grab")
{
Label2.Text = GridView1.SelectedRow.Cells[2].Text;
Label3.Text = GridView1.SelectedRow.Cells[3].Text;
Label4.Text = GridView1.SelectedRow.Cells[4].Text;
Label5.Text = GridView1.SelectedRow.Cells[5].Text;
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" CssClass="td" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="574px" onrowcommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/edit.png" OnClick="ImageButton2_Click" />
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Select" ImageUrl="~/images/delete.png" onclientclick=" return confirm('Are you want to Delete this Vehicle?');" />
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/images/refre.png" CommandName="Grab" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
<asp:BoundField DataField="Make" HeaderText="Make" SortExpression="Make" />
<asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
<asp:BoundField DataField="Submodel" HeaderText="Submodel" SortExpression="Submodel" />
<asp:BoundField DataField="ISENABLED" HeaderText="ISENABLED" SortExpression="ISENABLED" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
Try something like this. Since you're trying to access the values in the command event and not in the OnSelectedIndexChanged event you need to get hold of the row triggering the command event first.
if (e.CommandName == "Grab")
{
GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
if (row != null)
{
Label2.Text = row.Cells[2].Text;
Label3.Text = row.Cells[3].Text;
Label4.Text = row.Cells[4].Text;
Label5.Text = row.Cells[5].Text;
}
}
What I need to do is to highlight each row when it is processing to show the process progress, the gridview may contain almost one thousands of row. below is the code I have written but which doesn't work.
Please can someone help me.
<
asp:GridView ID="gdview1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Vertical" Font-Names="Calibri"
Font-Size="Small" AutoGenerateColumns="False"
OnRowDataBound="gdview1_RowDataBound"
OnSelectedIndexChanged="gdview1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkBxHeader" OnCheckedChanged="chkSelect_CheckedChanged" AutoPostBack="true" runat="server" />
</HeaderTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkNUM" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkNUM" runat="server" DataField="ColNUM" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Row#">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ColNUM" HeaderText="Contract #" />
<asp:BoundField DataField="Col1" HeaderText="Suffix" />
<asp:BoundField DataField="Col2" HeaderText="First Name" />
<asp:BoundField DataField="Col3" HeaderText="Last Name" />
<asp:BoundField DataField="Col4" HeaderText="Street" />
<asp:BoundField DataField="Col5" HeaderText="City" />
<asp:BoundField DataField="Col6" HeaderText="Zip" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void butChargeCreditCards_Click(object sender, EventArgs e)
{
DataTable tblContrts = (DataTable) Session["tblContrts"];
foreach (GridViewRow row in gdview1.Rows)
{
CheckBox chkbx = (CheckBox) row.FindControl("chkNUM");
if (chkbx != null && chkbx.Checked)
{
gdview1_SelectedIndexChanged(row,e);
string SS = chkbx.Text.ToString();
string strResults = method1;
}
}
}
protected void gdview1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gdview1.Rows)
{
if (row.RowIndex == gdview1.SelectedIndex)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
}
}
}
}
}
Have you tried it by using the "OnRowDataBound" or "OnRowCreated" Event of the Grid and write the color-highliting in code behind?
Something like this?
http://www.java2s.com/Code/ASP/ADO.net-Database/UsingtheRowCreatedEventtoprogrammaticallychangethestyle.htm
But maybe you have a problem because the loading would be too fast to really notice?