I have a parent and child gridview setup. on the OnRowDataBound event I need the value of a parent gridview cell. this is not the DataKeys of the parent Gridview. I have commented out some of the things I have tried, but not getting quite there. I think the last two lines are close, but I am not sure what the correct syntax should be.
gvCustomers is the parentGV.
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string requestId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select top 10 * from Orders where RequestID='{0}'", requestId));
gvOrders.DataBind(); // this is the childGV
string cell_1_Value = gvCustomers.Rows[e.Row.RowIndex].Cells[0].Text;
string cell_2_Value = gvCustomers.Rows[e.Row.RowIndex].Cells[1].Text;
}
}
This is now working using:
foreach (GridViewRow gr in gvCustomers.Rows)
{
string requestStatus = gvCustomers.Rows[gr.RowIndex].Cells[3].Text;
}
But is there anyway to get the same value using the name of the column?
You could try the following:
if (e.Row.RowType == DataControlRowType.DataRow)
{
string requestId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
gvOrders.DataSource = GetData(string.Format("select top 10 * from Orders where RequestID='{0}'", requestId));
gvOrders.DataBind(); // this is the childGV
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
string requestStatus = dr["myColumnHeader"].ToString();
}
}
Related
I am currently getting data from database, then if i checked the checkbox more than 2 rows, it will sum up the total of SENDQTY and send the details from GridView3 to GridView4. Now how can i check if the STOCKCODE from the 2 rows is different, it will prompt error message?
protected void showGridView()
{
int match = 0;
int total = 0;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[9] { new DataColumn("DODetailID"), new DataColumn("SALESORDERNO"), new DataColumn("STOCKCODE"), new DataColumn("SENDQTY"), new DataColumn("scanflag"), new DataColumn("REJECT"), new DataColumn("DispatchOrderID"), new DataColumn("DispatchDATE"), new DataColumn("DELORDERNO") });
foreach (GridViewRow row in GridView3.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox CheckBox1 = (row.Cells[0].FindControl("CheckBox1") as CheckBox);
//int qty = Convert.ToInt32(row.Cells[4].Text);
if (CheckBox1.Checked)
{
string DODetailID = (row.Cells[1].FindControl("DODetailID") as Label).Text;
string SALESORDERNO = (row.Cells[2].FindControl("SALESORDERNO") as Label).Text;
string STOCKCODE = (row.Cells[3].FindControl("STOCKCODE") as Label).Text;
int SENDQTY = Convert.ToInt32((row.Cells[4].FindControl("SENDQTY") as Label).Text);
CheckBox scanflag = (row.Cells[5].FindControl("scanflag") as CheckBox);
string REJECT = (row.Cells[6].FindControl("REJECT") as Label).Text;
string DispatchOrderID = (row.Cells[7].FindControl("DispatchOrderID") as Label).Text;
string DispatchDATE = (row.Cells[8].FindControl("DispatchDATE") as Label).Text;
string DELORDERNO = (row.Cells[9].FindControl("DELORDERNO") as Label).Text;
Session["STOCKCODE"] = STOCKCODE.ToString();
dt.Rows.Add(DODetailID, SALESORDERNO, STOCKCODE, SENDQTY, scanflag, REJECT, DispatchOrderID, DispatchDATE, DELORDERNO);
match++;
total = total + SENDQTY;
GridView4.DataSource = dt;
GridView4.DataBind();
}
}
}
}
protected void btnContinue_Click(object sender, EventArgs e)
{
showGridView();
mp3.Show(); // this is showing GridView4
}
you need to run two for each loops. Run the for/each loop on the data first time in which you count check boxes (and stock code). You can then in code decide to run/use your existing loop.
Collection MyCheckList = New Collection;
Boolean ListBad = false;
foreach (GridViewRow row in GridView3.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox CheckBox1 = (row.Cells[0].FindControl("CheckBox1") as CheckBox);
Label txtStock = (row.Cells[0].FindControl("STOCKCODE") as Label);
if CheckBox.Checked {
if MyCheckList.Contains(txtStock.Text) {
ListBad = true;
Exit for;
else
ListBad.Add(txtSTock)
}
etc. etc. etc
etc. etc.
In other words, run a test/loop over the data - put the stockcode into a collection if checked, and if such a stockcode already exists, then you know more then 1 checked of a given stock exists, and thus your flag ListBad = true when you finish that loop process.
You could also add the collection idea to your existing loop. And upon exit, you know if more then one stockcode was checked. So you probably don't need a separate first loop, but you do have to "build up" a list of checked + stockcodes if you checking for an already existing stock code. So build up a checked list in that collection and thus you know if a stockcode been used + checked in that list more then once.
I have a paging GridView in my ASP.NET project in which I do insertions of human resources into a database. My GridView loads everytime all the human resources inserted into the data base.
Now everytime I add a new row (human resource) or modify an existing one, I want it to be highlighted in the grid to make clear to the user that the operation was executed. I haven´t found a good way yet and the fact that the gridview is paged makes it more complex. I would appreciate some help :)
I'm adding the rows by binding de grid with a dataTable:
protected void llenarGrid() //se encarga de llenar el grid cada carga de pantalla
{
DataTable recursosHumanos = crearTablaRH();
DataTable dt = controladoraRecursosHumanos.consultarRecursoHumano(1, 0); // en consultas tipo 1, no se necesita la cédula
Object[] datos = new Object[4];
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
datos[0] = dr[0];
datos[1] = dr[1];
datos[2] = dr[2];
int id = Convert.ToInt32(dr[3]);
String nomp = controladoraRecursosHumanos.solicitarNombreProyecto(id);
datos[3] = nomp;
recursosHumanos.Rows.Add(datos);
}
}
else
{
datos[0] = "-";
datos[1] = "-";
datos[2] = "-";
datos[3] = "-";
recursosHumanos.Rows.Add(datos);
}
RH.DataSource = recursosHumanos;
RH.DataBind();
}
protected DataTable crearTablaRH()
{
DataTable dt = new DataTable();
dt.Columns.Add("Cedula", typeof(int));
dt.Columns.Add("Nombre Completo", typeof(String));
dt.Columns.Add("Rol", typeof(String));
dt.Columns.Add("Nombre Proyecto");
//dt.
return dt;
}
Store the Primary Key / A unique value which uniquely identifies the row in View State when you are inserting the row :
Let's assume the first Column has unique value. Add below line at the end of your llenarGrid() method.
ViewState["LastRowUniqueValue"] = datos[0];
Handle the Page_PreRender event, highlight the inserted row:
protected void Page_PreRender(object sender, EventArgs e)
{
string lastInsertedRowValue = string.Empty;
// only highlight the row if last inserted values are NOT a Hyphen -
if (ViewState["LastRowUniqueValue"] != "-")
{
// Assuming the Unique value is String, else cast accordingly
string lastInsertedRowValue = (string)ViewState["LastRowUniqueValue"];
int rowCnt = 0;
foreach (GridViewRow row in GridView1.Rows)
{
string CellText = row.Cells[0].Text;
if (CellText.Equals(lastInsertedRowValue))
{
row.Attributes.Add(“bgcolor”, “Yellow”);
break;
}
rowCnt++;
}
}
}
I use the rowdatabound event to find the row that has been edited and then assign a bootstrap css class to that row like this:
e.Row.CssClass = "danger";
Code:
DataSet ds = _dalEquipmentwiseCheckList.getEquipmentName();
ddEquipmentName.DataSource = ds.Tables[0].DefaultView;
ddEquipmentName.DataTextField = ds.Tables[0].Columns[1].ToString();
ddEquipmentName.DataValueField = ds.Tables[0].Columns[0].ToString();
ddEquipmentName.DataBind();
What I want is: when selecting a row in the GridView, the corresponding equipment name
should get selected in the dropdown list:
var selectRow = MyGridView.SelectedRow;
ddEquipmentName.SelectedValue = selectRow.Cells[2].Text;
****//this is giving me error****
Selected value does not work in this way. Try this:
ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));
You can try :
GridViewRow selectRow = MyGridView.SelectedRow;
ddEquipmentName.selectedItem.Text = selectRow.Cells[2].Text;
void setDataContext()
{
var selectRow = GridEquipmentChechList.MyGridView.SelectedRow;
if (selectRow != null)
{
txtECNumber.Text = selectRow.Cells[1].Text;
**ddEquipmentName.SelectedIndex = ddEquipmentName.Items.IndexOf(ddEquipmentName.Items.FindByText(selectRow.Cells[2].Text));**
**ddDescription.SelectedIndex = ddDescription.Items.IndexOf(ddDescription.Items.FindByText(selectRow.Cells[3].Text));**
}
}
I have a grid view, which contains a dropdownlist and a panel that i want to make invisible and visible by the selected value of the dropdownlist.
The javascript code which works when not used with the gridview is:
function showPannel(panelId,dropdownId ) {
var panel = document.getElementById(panelId);
var dropDown = document.getElementById(dropdownId);
if (dropDown.options[dropDown.selectedIndex].value = 'Diesel Deals') {
panel.className = "visibleDiv";
}
else{
panel.className = "hiddenDiv";
}
}
i'm passing the panelId and dropdownlist id from here:
if (e.Row.RowType == DataControlRowType.DataRow)
{
Panel p = (Panel)e.Row.FindControl("Panel1");
DropDownList t1 = (DropDownList)e.Row.FindControl("DropDownList1");
t1.Attributes.Add("onchange",
string.Format("javascript:showPannel('{0}', '{1}')",p.ClientID, t1.ClientID ));
}
but it is not working. The function is getting called, but its giving undefined when dropDown.options[dropDown.selectedIndex].value is alerted.
I tried to do
Gridview1 = document.getElementById('<%=GridView1.ClientID%>');
var cell = Gridview1.rows[0].cells[2];
var dropdownlist = cell.childNodes[0];
var dropdownSelectedValue = dropdownlist.options[dropdownlist.selectedIndex].value;
alert(dropdownSelectedValue);
but its not working either.
Please help
Thanks
Panel p = (Panel)e.Row.FindControl("Panel1");
DropDownList t1 = (DropDownList)e.Row.FindControl("DropDownList1");
string p_id = GridView1.ClientID + "_" + p.ClientID;
string ddL_id = GridView1.ClientID + "_" + t1.ClientID;
t1.Attributes.Add("onchange",
string.Format("javascript:showPannel('{0}', '{1}')", p_id, ddL_id ));
thanks for the hint about rendering id
I am trying to implement a checkbox within gridview,
The job of this checkbox is to verify a record,
When this verify button is pressed, all items with a checked checkbox are inputted into the database
This is my code:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cbox = ((CheckBox)row.FindControl("Verify"));
if (cbox.Equals(true))
{
String DraftsText = ((TextBox)row.FindControl("numDrafts")).Text;
String TCtext = ((TextBox)row.FindControl("numTC")).Text;
if (row.RowType == DataControlRowType.DataRow)
{
//Header trs = new Header();
// GridView1.Rows[0].FindControl("numTC");
if (TCtext != "" && DraftsText != "")
{
// try
// {
string date = row.Cells[4].Text;
DateTime dateTime = Convert.ToDateTime(date);
string dateFormatted = dateTime.ToString("d-MMM-yy");
string unit = row.Cells[5].Text;
string currency = row.Cells[6].Text;
string totalFC = row.Cells[7].Text;
string totalDC = row.Cells[8].Text;
int d = Convert.ToInt32(DraftsText);
int tc = Convert.ToInt32(TCtext);
hdr = new Header(d, tc, dateFormatted, unit, currency, totalFC, totalDC);
hdr.InsertFCTC(hdr);
}
}
}
}
}
I might be going at this the wrong way but in the if (cbox.Equals(true))
its giving me an exception: Object reference not set to an instance of an object.
Any idea what i can do to solve this?
Many Thanks
This if (cbox.Equals(true)) should be if (cbox.Checked)
Since your cbox is a checkbox object it can't be used to compare, so you have to use the cbox Checked Property, which will return true/false
You receive a NullPointerException because the suggested checkbox wasn't found! Or the direct cast into an instance of type CheckBox doesn't worked as expected.
Change your code to something like this and retry:
CheckBox cbox = ((CheckBox)row.FindControl("Verify"));
if (cbox != null && cbox.Checked)
{
....
}