Get value from gridview CheckBoxField on rowcommand event - asp.net

I have a column in gridview as following
<asp:CheckBoxField DataField="IsProcessed" HeaderText="HQ Response">
<ItemStyle Width="100px" />
</asp:CheckBoxField>
On gridview rowcommand event i have the following code-
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int requisitionId = Convert.ToInt32(e.CommandArgument);
CheckBox cbox = (CheckBox)row.FindControl("IsProcessed"); //does not work
But cbox is returning null. What wrong i am doing here?

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int requisitionId = Convert.ToInt32(e.CommandArgument);
CheckBox cbox = (CheckBox)row.Cells[3].Controls[0];

try this
GridDataItem item = (GridDataItem)e.Item;
CheckBox cbox =(CheckBox)item.FindControl("IsProcessed");

Here are a couple of methods to expand on s.k.paul's answer.
One method gets a column value and one gets the bool value of a checkbox. The other method finds the column based on the heading.
public static bool gvGetCBVal(GridView gvGrid, string sHeaderText)
{
GridViewRow row = gvGrid.SelectedRow;
int iCol = gvGetColumn(gvGrid, sHeaderText);
CheckBox ckBox = (CheckBox)row.Cells[iCol].Controls[0];
return ckBox.Checked;
}
public static string gvGetVal(GridView gvGrid, string sHeaderText)
{
GridViewRow row = gvGrid.SelectedRow;
int iCol = gvGetColumn(gvGrid, sHeaderText);
return row.Cells[iCol].Text;
}
private static int gvGetColumn(GridView gvGrid, string sHeaderText)
{
int iRetVal = -1;
for (int i = 0; i < gvGrid.Columns.Count; i++)
{
if (gvGrid.Columns[i].HeaderText.ToLower().Trim() == sHeaderText.ToLower().Trim())
{
iRetVal = i;
}
}
return iRetVal;
}

Related

How to determine which button is clicked in dynamically created DataTable

I'm creating dynamically data Table bound to Grid View. Every row is populated with button. When i determine which button is clicked on the row, i want to get the current value of cell in clicked button in that row and modify her.Help!
Markup
<asp:GridView ID="GridView2" runat="server"
OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnTest" runat="server"
CommandName="odzemi"
CssClass="button2"
Font-Bold="True"
Text="-"
Width="100px"
OnClick="btnTest_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
//in this way a create row
private void AddNewRecordRowToGrid()
{
int counter;
if (Request.Cookies["kasa"] == null)
counter = 0;
else
{
counter = int.Parse(Request.Cookies["kasa"].Value);
}
counter++;
Response.Cookies["kasa"].Value = counter.ToString();
Response.Cookies["kasa"].Expires = DateTime.Now.AddYears(2);
if (ViewState["Markici"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
HttpCookie cookie = Request.Cookies["Democookie"];
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["MarkicaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["Datum"] = DateTime.Now;
drCurrentRow["Masa"] = Session["masa39"];
drCurrentRow["VrabotenID"] = Session["New"];
drCurrentRow["Artikal"] = Label3.Text;
drCurrentRow["Cena1"] = Label4.Text;
//this is where i need to make changes
drCurrentRow["Kolicina"] = Label5.text;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label6.Text;
drCurrentRow["VkDanok"] = Label8.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label23.Text;
drCurrentRow["Edmera"] = Label10.Text;
drCurrentRow["ArtikalID"] = Label33.Text;
}
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//Added New Record to the DataTable
dtCurrentTable.Rows.InsertAt(drCurrentRow,0);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
}
//in this button i call that method
protected void Button9_Click(object sender, EventArgs e)
{
AddNewRecordRowToGrid();
}
//
and here i'm determine clicked button of row, and here in this
dtCurrentTable.Rows[clickedIndex]["Kolicina"]= "" i need to
get cureent value of cell which is different in every row.
protected void btnTest_Click(object sender, EventArgs e)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
var clickedIndex = clickedRow.RowIndex;
//so here i want to get current value of cell, and modify her
dtCurrentTable.Rows[clickedIndex]["Kolicina"] = " ";
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
Try this.
protected void btnTest_Click(object sender, EventArgs e)
{
var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
var clickedIndex = clickedRow.RowIndex;
}
Provided your GridView does not have sorting or paging enabled, from this clickedIndex, we can get the corresponding Row of the DataTable like this.
dtCurrentTable.Rows[clickedIndex]["Kolicina"] = "StackOverflow";
In the RowDataBound event, the EventArgument e should have the index of the current item bound in it. The code e.Row.RowIndex will return the index of the row in the Grid, not necessarily the datasource when you have paging applied. There is a 'DataItemIndex' property, another index that may give you what you are looking for, depending on what you are trying to do.
The grid also exposes the Rows collection, which you can use a foreach to loop through each one, and access that way too.
Note, the Rows on postback will only be based on the markup posted back through viewstate; the grid knows nothing about your data source (unless you rebind on every postback).

dynamic data binding gridview

my datatable formed as follows
DataTable dtFinalData = new DataTable();
//Adding columns for BR details
dtFinalData.Columns.Add("SupId", typeof(string));
dtFinalData.Columns.Add("SupName", typeof(string));
DateTime dt = DateTime.Today;
int num = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
//--> adding columns for date part (1-31)
for (int i = 1; i < num + 1; i++)
{
dtFinalData.Columns.Add(i.ToString(), typeof(bool));
}
//<-- adding columns for date part (1-31)
#endregion
#region Fill dtFinalData
//--> Looping each BR from tblBrList
foreach (DataRow BrRow in dtBrList.Rows)
{
DataRow dr = dtFinalData.NewRow();
int SupId = Convert.ToInt32(BrRow[0]); //retrieve BR ID from dtBrList
String supName = BrRow[1].ToString(); //retreive Supervisor name from dtBrList
dr["SupId"] = SupId.ToString();
dr["SupName"] = supName;
for (int i = 1; i < num + 1; i++)
{
DateTime dtRunningDate = new DateTime(2013, 5, i);
//select BR_SUP_CODE,
DataRow[] drAttendance = dtAttendance.Select("BR_SUP_CODE=" + SupId + " AND SMS_DATE=#" + dtRunningDate + "#", string.Empty);
if (drAttendance.Length == 1)
{
//CheckBox chkbx = new CheckBox();
//chkbx.Checked = true;
dr[i.ToString()] = true;
}
else
{
//CheckBox chkbx = new CheckBox();
//chkbx.Checked = false;
dr[i.ToString()] = false;
}
}
dtFinalData.Rows.Add(dr);
}
//<-- Looping each BR from tblBrList
#endregion
GridView1.DataSource = dtFinalData;
GridView1.DataBind();
Now i want to add checked image in place of true and unchecked image in place of false.how to bind grid view dynamically such that in place of disable check box i want to insert two types of image?
Your DataTable part is fine and continue to add the True/False text as per the logic. Now you should handle the GridView part. So, define an event handler for the OnRowDataBound event of GridView.
In this event only, check for the Text property of the cells, and if True/False, clear the cells and add the required image.
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" ... />
And your event handler will have code as below:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Image chkImage = new Image();
chkImage.ImageUrl="~/images/Checked.png";
Image UnchkImage = new Image();
UnchkImage.ImageUrl = "~/images/UnChecked.png";
if (e.Row.RowType == DataControlRowType.DataRow)
{
// We will start from 3rd cell because the first 2 cells are
// for SupID & SupName, where we don't need to place images
for (int cellIndex = 2; cellIndex < GridView1.Columns.Count; cells++)
{
// Add Checked Image when cell text is true
if (e.Row.Cells[cellIndex].Text == "true")
{
// clear the cell and add only the image
e.Row.Cells[cellIndex].Controls.Clear();
e.Row.Cells[cellIndex].Controls.Add(chkImage);
}
// Add Unchecked Image when cell text is false
if (e.Row.Cells[cellIndex].Text == "false")
{
// clear the cell and add only the image
e.Row.Cells[cellIndex].Controls.Clear();
e.Row.Cells[cellIndex].Controls.Add(unchkImage);
}
}
}
}

How to find child gridview in user defined function

This is my code to save the selected check box values while paging, but as I am working with nested gridview I am unable to find the control of the required child gridview
private void SaveCheckedValues()
{
ArrayList userdetails = new ArrayList();
int index = -1;
GridView gv = (GridView)gvCustomers.FindControl("gvOrders"); // Is this correct or any other way of finding the child control
foreach (GridViewRow gvrow in gv.Rows)
{
index = (int)gv.DataKeys[gvrow.RowIndex].Value;
bool result = ((CheckBox)gvrow.FindControl("chkBoxChild")).Checked;
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
userdetails = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!userdetails.Contains(index))
userdetails.Add(index);
}
else
userdetails.Remove(index);
}
if (userdetails != null && userdetails.Count > 0)
Session["CHECKED_ITEMS"] = userdetails;
}
I have a generic recursive find control code that often helps in these circumstances. The issue with grid controls is that there is a certain level of nesting of controls int hem for the row and cell, and contents in the cell.
Private Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
If root.ClientID Is Nothing AndAlso root.ClientID.EndsWith(id) Then
Return root
End If
For Each c As Control In root.Controls
Dim t As Control = FindControlRecursive(c, id)
If Not t Is Nothing Then
Return t
End If
Next c
Return Nothing
End Function
The code is in VB.net but you get the gist
private void SaveCheckedValues()
{
ArrayList userdetails = new ArrayList();
int index = -1;
foreach (GridViewRow gvRow1 in gvCustomers.Rows)
{
GridView gv = (GridView)gvRow1.FindControl("gvOrders");
foreach (GridViewRow gvrow in gv.Rows)
{
index = (int)gv.DataKeys[gvrow.RowIndex].Value;
bool result = ((CheckBox)gvrow.FindControl("chkBoxChild")).Checked;
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
userdetails = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!userdetails.Contains(index))
userdetails.Add(index);
}
else
userdetails.Remove(index);
}
}
if (userdetails != null && userdetails.Count > 0)
Session["CHECKED_ITEMS"] = userdetails;
}
try this:
private void SaveCheckedValues()
{
foreach(GridViewRow rIndex in GridView1.Rows)
{
GridView gv = new GridView();
gv = (GridView)row.FindControl("GridView2");
//user gv
}
}

How to access a gridview column on rowdatabound ?

I would like to change the value of my gridview column to active when the value is 1.
I have gridview column like
<asp:BoundField DataField="STATUS" HeaderText="STATUS" SortExpression="STATUS" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
and cs code
protected void gvCategory_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[5].Text=="0")
{
e.Row.Cells[5].Text = "INACTIVE";
}
}
}
This is working but it would fail if i change the column order.
What i need is something like findControl function.
Thanks.
Here's a few utilities I wrote years ago that may help you:
// ---- GetCellByName ----------------------------------
//
// pass in a GridViewRow and a database column name
// returns a DataControlFieldCell or null
static public DataControlFieldCell GetCellByName(GridViewRow Row, String CellName)
{
foreach (DataControlFieldCell Cell in Row.Cells)
{
if (Cell.ContainingField.ToString() == CellName)
return Cell;
}
return null;
}
// ---- GetColumnIndexByHeaderText ----------------------------------
//
// pass in a GridView and a Column's Header Text
// returns index of the column if found
// returns -1 if not found
static public int GetColumnIndexByHeaderText(GridView aGridView, String ColumnText)
{
TableCell Cell;
for (int Index = 0; Index < aGridView.HeaderRow.Cells.Count; Index++)
{
Cell = aGridView.HeaderRow.Cells[Index];
if (Cell.Text.ToString() == ColumnText)
return Index;
}
return -1;
}
// ---- GetColumnIndexByDBName ----------------------------------
//
// pass in a GridView and a database field name
// returns index of the bound column if found
// returns -1 if not found
static public int GetColumnIndexByDBName(GridView aGridView, String ColumnText)
{
System.Web.UI.WebControls.BoundField DataColumn;
for (int Index = 0; Index < aGridView.Columns.Count; Index++)
{
DataColumn = aGridView.Columns[Index] as System.Web.UI.WebControls.BoundField;
if (DataColumn != null)
{
if (DataColumn.DataField == ColumnText)
return Index;
}
}
return -1;
}
You could loop all columns to get the correct index or use this LINQ:
String colToFind = "status";
int colIndex = ((GridView)sender).Columns.Cast<DataControlField>()
.Where((c, index) => c.HeaderText.ToLower().Equals(colToFind))
.Select((c,index)=>index).First();
Cleaner easier to read LINQ. Tim's did not work for me.
private int GetFirstGridViewColIndex(string dataField, object sender)
{
var boundFieldColumns = ((GridView)sender).Columns.Cast<BoundField>();
return boundFieldColumns.Where((column, index) => string.Equals(column.DataField, dataField, StringComparison.InvariantCultureIgnoreCase))
.Select((column, index) => index)
.First();
}

change a pageDataSource page

Hi all i have a page that takes items from a sql datasource and puts them into a repeater. I want to know how to change the page index of the pagedDatasource when i click another button
page load
DataSourceSelectArguments arg = new DataSourceSelectArguments();
arg.MaximumRows = 8;
arg.AddSupportedCapabilities(DataSourceCapabilities.Page);
DataView set = (DataView)SQLDataSourceProducts.Select(arg);
int rows = arg.TotalRowCount;
PagedDataSource paged = new PagedDataSource();
paged.DataSource = set;
paged.AllowPaging = true;
paged.PageSize = 3;
int pageIndex = 1;
paged.CurrentPageIndex = pageIndex - 1;
RepeaterProducts.DataSource = paged;
RepeaterProducts.DataBind();
On a button press
protected void moreButton_Click(object sender, EventArgs e)
{
//Change the pageIndex
}
Thanks all
Hi all found the answer and this is how i done it for anyone who wants to know
public void popRepeater()
{
//Clear datasource so you can repopulate without getting duplication error
SQLDataSourceProducts.SelectParameters.Clear();
//Your query
SQLDataSourceProducts.SelectParameters.Add
DataSourceSelectArguments arg = new DataSourceSelectArguments();
arg.MaximumRows = 8;
arg.AddSupportedCapabilities(DataSourceCapabilities.Page);
DataView set = (DataView)SQLDataSourceProducts.Select(arg);
int rows = arg.TotalRowCount;
PagedDataSource paged = new PagedDataSource();
paged.DataSource = set;
paged.AllowPaging = true;
paged.PageSize = 3;
//int pageIndex = 1;
paged.CurrentPageIndex = CurrentPage ;
RepeaterProducts.DataSource = paged;
RepeaterProducts.DataBind();
}
public int CurrentPage
{
get
{
// look for current page in ViewState
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0; // default page index of 0
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void moreButton_Click(object sender, EventArgs e)
{
CurrentPage += 1;
popRepeater();
}

Resources