Trigger GridView1_SelectedIndexChanged event in pageload if session is == to column - c#

I am trying to use selectedindex on my gridview if it has a column with an == value to session.
Right now I am only successfull on changing its background color. But how could I also trigger the button If it has similar value on my Session.
private void load_session_value()
{
string trans_id = Session["transaction_id_report"].ToString();
string trans_number = Session["transaction_no_report"].ToString();
//string grid_value_id = GridView1
//string grid_value_num
if (trans_id != null && trans_number != null)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.Cells[1].Text.ToString() == trans_id && row.Cells[2].Text.ToString() == trans_number)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
GridView1_SelectedIndexChanged(new object(),new EventArgs());
}
}
}
}
This is my current output during page_load

I'm not sure, but i think you are looking for this.
if (row.Cells[1].Text.ToString() == trans_id && row.Cells[2].Text.ToString() == trans_number)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
//call the Button1_Click method
Button1_Click(new object(), new EventArgs());
}
protected void Button1_Click(object sender, EventArgs e)
{
//do the button click stuff
}

Related

ASP.NET get checked rows values from paginated gridview

I have created a paginated grid view which contains check boxes for multiple selection.But while looping through the data-row in the grid-view only last pages row values only I am able to get.I know it's because of the pagination.
My code is below
protected void btnSaveItemMapping_Click(object sender, EventArgs e)
{
grdCustomers.AllowPaging = false;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
if (myCheckBox.Checked == true)
{
Label lblCustomerCode = (Label)gvrow.FindControl("lblCustomerCode");
//INSERT TO DATABSE
}
}
grdCustomers.AllowPaging = true
}
Keeping check box checked after changing page index like below code.
protected void grdCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
savechkdvls_cst();
grdCustomers.PageIndex = e.NewPageIndex;
if (Session["AllCustomers"] != null)
{
lstAllCustomers = (List<CustomerMaster>)Session["AllCustomers"];
}
grdCustomers.DataSource = lstAllCustomers;
grdCustomers.DataBind();
chkdvaluesp_cst();
}
private void chkdvaluesp_cst()
{
ArrayList usercontent = (ArrayList)Session["chkditems_customers"];
if (usercontent != null && usercontent.Count > 0)
{
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
int index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
if (usercontent.Contains(index))
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
myCheckBox.Checked = true;
}
}
}
}
private void savechkdvls_cst()
{
ArrayList usercontent = new ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
bool result = ((CheckBox)gvrow.FindControl("chkBoxBrandCustomers")).Checked;
// Check in the Session
if (Session["chkditems_customers"] != null)
usercontent = (ArrayList)Session["chkditems_customers"];
if (result)
{
if (!usercontent.Contains(index))
usercontent.Add(index);
}
else
usercontent.Remove(index);
}
if (usercontent != null && usercontent.Count > 0)
Session["chkditems_customers"] = usercontent;
}
Any help will be appreciated.

Hide GridView Image Column if Database value is Null

I have a Gridview which has an image column to the right side.When the user checks a checkbox only those items with a non Null Image should be displayed.
I have seen that the Gridview uses the default Image if the database image corresponding to the row is empty.Will i need to write a new stored procedure for this or is there a better way to do it.
I currently have implemented this
try
{
if (checkBox1.Checked == true)
{
dgvGetData.Columns["image"].Visible = true;
foreach (DataGridViewRow row in dgvGetData.Rows)
{
Console.WriteLine("LOOP");
if (row.Cells[16].Value == null)
{
Console.WriteLine("######################################> NULL");
row.Visible = false;
}
else
{
Console.WriteLine("######################################> NOT NULL");
}
}
}
else
{
dgvGetData.Columns["image"].Visible = false;
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
I'd recommend that you use the CellFormatting event handler. You can write a few lines of code to determine if you're on the correct column and then have it display whatever you want.
Here is a partial example:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value == null && dataGridView1.Columns[e.ColumnIndex].Name == "Image")
{
dataGridView1.Rows[e.RowIndex].Visible = false;
}
}
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellformattingeventhandler(v=vs.110).aspx

`DataGridViewComboBoxCell` returns `null`

The first cell of my DataGridView is ComboBox. I am adding members in this column like below...
DataTable dt = new DataTable();
string qry = "SELECT [NAME] FROM [PERSONS]";
// running ExcecuteNonQuery() function in globalData.cs file
dt = globalData.q.select(qry, globalData.connectionstring);
foreach (DataRow row in dt.Rows)
{
(this.dataGrid.Columns["Name"] as DataGridViewComboBoxColumn).Items.Add(row[0].ToString());
}
and checking on Cell_Leave event
if ((this.dataGrid.CurrentRow.Cells[0] as DataGridViewComboBoxCell).Value == null)
{
MessageBox.Show("You must select one option.");
}
But the value returns null every time even though the value is selected from the ComboBoxCell. Here Cell is not null but the value of the cell is null.
What's wrong with this?
I achieved this like below....
private void DataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dataGrid.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.KeyDown += this.DataGridComboBox_KeyDown;
}
}
private void DataGridComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (!(e.Control && e.KeyCode == Keys.S) && !(e.Control && e.KeyCode == Keys.C))
{
try
{
var currentcell = this.dataGrid.CurrentCellAddress;
var sendingCB = sender as DataGridViewComboBoxEditingControl;
DataGridViewComboBoxCell cel = (DataGridViewComboBoxCell)this.dataGrid.Rows[currentcell.Y].Cells[0];
cel.Value = sendingCB.EditingControlFormattedValue.ToString();
}
catch { }
}
}

repositoryitemhyperlinkedit show/hide according to the row data

I am using an XtraGridView control in my winform. Now I added a RepositoryItemHyperLinkEdit to it. But I want to show/hide each link according to the row data.
How can I achieve this?
Thanks for any help..
I tried the next code but it did not work, the cell did not be empty.
("Show link" part is ok, but String.Empty does not work)
private void xgvGrid_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if (e.Column == gcControlField)
{
if (xgvGrid.GetFocusedRowCellValue("ControlField") != null)
{
if (xgvGrid.GetFocusedRowCellValue("ControlField").ToString() == "LINK")
e.DisplayText = "Show link";
else
e.DisplayText = string.Empty;
}
}
}
You can add your checking in the event GridView.CustomColumnDisplayText.
e.g. Each row is bind to a Person instance
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
// gcLink is your column using repositoryitemhyperlinkedit
if (e.Column == gcLink)
{
var person = gridView1.GetRow(e.RowHandle) as Person;
if (person != null)
{
// Logic to show/hide the link based on other field
if (person.FirstName == "John")
e.DisplayText = string.Empty;
else
e.DisplayText = person.Link;
}
}
}

How can I access the row in a RowCommand event

I am trying to find a DropDown element in the GridView_RowCommand but it says that GridViewCommandEventArgs does not contain a definituon for 'Row'. I need to do this in this event because i am evaluating a GridView Command. See failing code below
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
DropDownList Myddl = null;
ClientClass client = new ClientClass();
Myddl = e.Row.FindControl("ddlClients") as DropDownList;
if (Myddl != null)
{
updated = client.InsertUpdateClient(ClientID,
int.Parse(e.CommandArgument.ToString()), departmentID);
}
else
{
Labels.Text = "There was an error updating this client";
}
}
}
Something like this:
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
This is assuming what's firing off the RowCommand is a LinkButton. Change that according.
In addition to the #Stephen,
if (e.CommandName == "Add")
{
DropDownList Myddl = null;
ClientClass client = new ClientClass();
//Use this if button type is linkbutton
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
//Use this if button type is Button
//GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Myddl = row.FindControl("ddlClients") as DropDownList;
if (Myddl != null)
{
updated = client.InsertUpdateClient(ClientID,
int.Parse(e.CommandArgument.ToString()), departmentID);
}
else
{
Labels.Text = "There was an error updating this client";
}
}

Categories

Resources