Getting gridview data in variables and inserting it to database - asp.net

I am making a shopping cart type of thing using grid view user will keep adding item in it after doing it so user will click save i want to get values from all columns of grid view each row at a time and save it to database.
here is the code for grid view load
protected void add_Click(object sender, EventArgs e)
{
product.Item = Item_Drop.SelectedItem.Text;
product.Quantity = quantity_box.Text;
int qun =Convert.ToInt32(quantity_box.Text);
int unitP= Convert.ToInt32(unitPrice_box.Text);
product.item_Toal = (qun*unitP).ToString();
list.Add(product);
temp_gridView.DataSource = list;
temp_gridView.DataBind();
}
And here is what i am trying to get values
public void Values_from_grid()
{
foreach(temp_gridView row in temp_gridView.Rows)
{
for(int i = 0; i < temp_gridView.Columns.Count, i++)
{
String header = temp_gridView.Columns[i].HeaderText;
String cellText = row.Cells[i].Text;
}
}
}
i am not getting any values in "header" or "cellText" . .. .

Use this code to get header row value .
GridViewName.HeaderRow.Cells[0].Text;

Found Answer no need to use
GridView.Rows.Count
just use a simple int to iterate
int i = 0;
foreach(GridViewRow row in temp_gridView.Rows)
{
box.Item_id = temp_gridView.Rows[i].Cells[0].Text;
box.Quantity = temp_gridView.Rows[i].Cells[1].Text;
box.Total = temp_gridView.Rows[i].Cells[2].Text;
i++;
}

Related

updating view from gridview

My query is
SELECT
deldate,
transno,
matno,
MAT_NAME,
rawpkgno,
MAX(SWITCH(deldate=? ,ORDCASES )) AS [CURRDATE],
MAX(SWITCH(deldate=? ,ORDCASES )) AS [previous_day],
MAX(SWITCH(deldate=? ,ORDCASES )) AS [Last_date]
FROM
invorder
WHERE
invorder.strno =54009
OR [invorder.deldate] IS NULL
GROUP BY
matno,
MAT_NAME,
rawpkgno,
transno,
deldate
I want to edit this from gridview. As this is view I cannot edit it from gridview. But is their any way through which previous_day, Last_date columns can be constant and I can only make changes to CURRDATE column. Kindly help me.
Hi you have to bind the row_added event on grid and there make the all the cell read only except u want editable.. check the below code..
public Form1()
{
InitializeComponent();
dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
}
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
foreach (DataGridViewCell item in dataGridView1.Rows[e.RowIndex].Cells)
{
// here i used the 5 because i want to make the 5th index cell as editable.
if (item.ColumnIndex == 5)
{
continue;
}
item.ReadOnly = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bind_your_data_source_here;
dataGridView1.Refresh();
}
you can find the sample from here..
Download

ASPxComboBox Filtering Large DataSet Using XpoDataSource (DevExpress)

I tried to Filter an ASPxComboBox that gets data using an XpoDataSource, note that restoring and filtering data from a small data set works fine , the issue start when I try to filter large dataset - about 70000 records- from datasource the ComboBox loading becomes very slow since XpoDataSource gets all data from database table .
So I created a criteria for the XpoDataSource to reduce number of records restored ,then the ComboBox Keeps repeating the top 10 records while scrolling down the ComboBox, I don't know where the problem is.
I realized that what I need is similar to the example in the following link
But using an XpoDataSource instead of SqlDataSource1 .
I don't know how to write a similar code for an XpoDataSource .
this is my code :
protected void cmbServices_OnItemRequestedByValue_SQL(object source, DevExpress.Web.ASPxEditors.ListEditItemRequestedByValueEventArgs e)
{
try
{
string criteria = "";
if (string.IsNullOrEmpty(e.Value.ToString()) || e.Value.ToString().Length < 3)
{
criteria = "1 = 2";
}
else
{
criteria =
string.Format("(( Code like '{0}%' OR ProductName like '{0}%') AND CustomerId = {1})", e.Value.ToString(), (cmbServicesActivities != null && cmbServicesActivities.Value != null) ? cmbServicesActivities.Value.ToString() : "0");
}
dsServices.Session = LookupsSession;
dsServices.Criteria = criteria;
cmbServicesDescription.DataSource = dsServices;
cmbServicesDescription.DataBind();
}
catch (Exception exc)
{
Debug.WriteLine(exc.Message);
}
}
The Following Example demonstrates the answer of my question
public partial class _Default : System.Web.UI.Page {
Session session = XpoHelper.GetNewSession();
protected void cmb_ItemRequestedByValue(object source, DevExpress.Web.ASPxEditors.ListEditItemRequestedByValueEventArgs e) {
MyObject obj = session.GetObjectByKey<MyObject>(e.Value);
if (obj != null) {
cmb.DataSource = new MyObject[] { obj };
cmb.DataBindItems();
}
}
protected void cmb_ItemsRequestedByFilterCondition(object source, DevExpress.Web.ASPxEditors.ListEditItemsRequestedByFilterConditionEventArgs e) {
XPCollection<MyObject> collection = new XPCollection<MyObject>(session);
collection.SkipReturnedObjects = e.BeginIndex;
collection.TopReturnedObjects = e.EndIndex - e.BeginIndex + 1;
collection.Criteria = new BinaryOperator("Title", String.Format("%{0}%", e.Filter), BinaryOperatorType.Like);
collection.Sorting.Add(new SortProperty("Oid", DevExpress.Xpo.DB.SortingDirection.Ascending));
cmb.DataSource = collection;
cmb.DataBindItems();
}

How to get values from Gridview column

I have an editable GridView that emails it's content to all companies. The first row of the Gridview contains the company name (just 1 name per row). I want to email the Gridview to a specific email address based off of the company name in the first row. e.g - if first row equals companyname1 then send the email to company one; if first row equal company two, then send the email to company two. I attempted the following:
//C# - code snippet behind button
foreach (GridView item in gvCompanies.Rows)
{
if (item.SelectedRow.Cells[1].Text == "company1")
{
txtEmailAddresses.Text = "company1#gmail.com";
}
else if (item.SelectedRow.Cells[1].Text == "company2")
{
txtEmailAddresses.Text = "company2.com";
}
else if (item.SelectedRow.Cells[1].Text == "company3")
{
txtEmailAddresses.Text = "company3#aol.com";
}
else if (item.SelectedRow.Cells[1].Text == "company4")
{
txtEmailAddresses.Text = "company#aol.com";
}
}
...Could anyone provide some guidance as to what am doing wrong here?
Try Something like this...
protected void gvCompanies_SelectedIndexChanged(object sender, EventArgs e)
{
//for (int i = 0; i < gvCompanies.Rows.Count; i++)
// {
if (gvCompanies.SelectedIndex == i)
{
String compName = gvCompanies.Rows[i].Cells[1].Text; //Gets the data cell value from the grid to string
if(compName == "company1")
{
txtEmailAddresses.Text = "company1#gmail.com";
}
else if(compName == "company2")
{
txtEmailAddresses.Text = "company2#gmail.com";
}
........and so on
}
// }
}
You need to set the folowing 2 properties of gridview.
1] AutoGenerateSelectButton="true" and
2] onselectedindexchanged="GridView1_SelectedIndexChanged".

Binding LookupEdit Repository

I've three lookupedit repositories (for three columns) in a devexpress xtragrid control. I want to bind lookupedit2 based on a value selected from lookupedit1 and lookupedit3 based on lookupedit2. I can only see the populated elements when I click on the lookupedit. However, it doesn't display the selected element on the grid.
Here is the code i used:
void repositoryLookupEdit1_EditValueChanged(object sender, EventArgs e) {
LookUpEdit edit = gridView.ActiveEditor as LookUpEdit;
int val = Convert.ToInt32(edit.EditValue);
if (!val.Equals(0)) {
var elements = from e in dc.Elements select e;
repositoryLookupEdit1.DisplayMember = "paymentType";
repositoryLookupEdit1.ValueMember = "paymentTypeID";
repZone.DataSource = bindData(elements);
}
}
public BindingSource bindData(object obj) {
BindingSource ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
What could be the possible problem?
I believe this example will be helpful:
How to filter a second LookUp column based on a first LookUp column's value

ASP.net problem with changing table row visibility

i have a table array dynamically generated from a data query and stored in a session variable. Now i wanna add a textbox to limit how many rows at a time i will display. To test this, i wrote two button methods, one will set some rows to be visible = false, and the second button method will set the same rows back to visible = true.
protected void limit_btn_Click(object sender, EventArgs e)
{
for (int i = 0; i < traceTables.Length; i++)
for (int j = 2; j < traceTables[i].Rows.Count; j++)
traceTables[i].Rows[j].Visible = false;
Session["Tables"] = traceTables;
table_C();
}//end limit_btn_Click()
protected void obo_btn_Click(object sender, EventArgs e)
{
for (int i = 0; i < traceTables.Length; i++)
for (int j = 2; j < traceTables[i].Rows.Count; j++)
traceTables[i].Rows[j].Visible = true;
Session["Tables"] = traceTables;
table_C();
}//end obo_btn_Click()
protected void table_C()
{
String changeTo = log_locations.SelectedValue;
for (int i = 0; i < sshLoc.Length; i++)
{
if (sshLoc[i].CompareTo(changeTo) == 0)
{
table_panel.ContentTemplateContainer.Controls.Remove(traceTables[currentTable]);
System.Diagnostics.Debug.WriteLine("Removing " + sshLoc[currentTable]);
table_panel.ContentTemplateContainer.Controls.Add(traceTables[i]);
System.Diagnostics.Debug.WriteLine("Adding " + sshLoc[i]);
currentTable = i;
Session["CurrentTable"] = currentTable;
break;
}//end if
}//end for
}//end table_C()
table_C() basically removes and adds the table from the panel - i use it when i want to switch between tables from a dropdown list (which works) and in this case it simply removes and adds the same table from the panel content container.
The problem is that setting the rows to be not visible works fine. Setting the rows back to visible never does, and i'm not sure why
Try using display:none and display:visible rather than .visible in ASP
traceTables[i].Rows[j].Add("style","display:none");
Visible removes it completely from the HTML, so you can only show it again by recreating the page.
You need to store data in a list rather than in a table.
When you set the rows to not visible they are being removed from the html table. That is why you cannot set them to visible again.
If you store the data in a seperate object and bind it to the list, you will be able to turn visibility on and off.

Resources