Solved

How to find the Datarow of a datagrid?

Posted on 2004-10-20
10
878 Views
Last Modified: 2012-06-27
Hi There,
I have a datagrid with 4 columns. I need to update a row. For that, I am doing this..

DataRow[] adrEdit = dsOvertureAccounts.Tables["AllAccountConstraints"].Select("Account='"+dgAllAccountConstraints.DataKeys[e.Item.ItemIndex] + "'");

This is returning an array of datarows. But how do I find out which row to update??

Any suggestions appreciated!!
Thanks
0
Comment
Question by:jackrock79
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 12361207
if your datakey is a unique key, the row you need to update should always be the first one.
0
 
LVL 2

Author Comment

by:jackrock79
ID: 12361225
Unfortunately, the datakey is not unique. That is the reason I am getting array of rows for the selection criteria.
Thanks.
0
 
LVL 33

Expert Comment

by:raterus
ID: 12361276
Your datakeyfield should be unique, it really serves no purpose if it isn't.  Can you put use another unique field?
0
 
LVL 2

Author Comment

by:jackrock79
ID: 12361350
Yeah that's what I am planning to. Put a invisible column with Rowid number and make it the datakeyfield. But I have to number the RowId. I am thinking how I can automatically assign rowid number to that column. Probably shud do it in the Item databound?
Thanks.
0
 
LVL 33

Expert Comment

by:raterus
ID: 12361430
Your data should be presented to the datagrid with a unique key already in place.  Even if you fumbled through what you propsed, you still are probably going to need to update the original database eventually..right?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:jackrock79
ID: 12362035
Do you know how to dynamically add a new column with row values equal to the rows in the datagrid??
Thanks.
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12363459
Raterus is pointing you in the right direction here.  It will help to know what your data source is (SQL Server? Access?).  If it either SQL or Access, you can have an Auto Numbering field for your unique id.  Make sure your data table also has this column and you are returning it in your Select statement.  Then in your grid, turn AutoGenerateColumns to false, and add each column manually.  If you open the Properties window for the grid, you will see a Columns property with a "..." button.  Click this for a dialog that will allow you to do this.  Add the auto num column first, and set uncheck the Visible check box.  The column will be available as a datakey and as the first Cell in a DataGridItem.Cells collection, but it will not show in the browser.  Then do your Select statement using this value and you wil always get one row in the array.

John
0
 
LVL 2

Author Comment

by:jackrock79
ID: 12371747
Hi John And Raterus,
This is what I did..and my dB is Sql Server 2000. It doesnt update the dB(doesnt even call the procedure)

SqlCommand cmdOverture = new SqlCommand("dbo.sp_AgentUIGetAllAccountConstraints",GetConnection("Overture").Connection);
                        cmdOverture.CommandType = CommandType.StoredProcedure;
                        cmdOverture.CommandTimeout=300;

                        adapter.SelectCommand = cmdOverture;
                        adapter.Fill(dsOvertureAccounts, "AllAccountConstraints");

                        //Session["SessionDataSetOvAccounts"]= dsOvertureAccounts;
                        

                        DataTable dt = dsOvertureAccounts.Tables[0];
                        dt.Columns.Add(new DataColumn("Identity",Type.GetType("System.Int32")));
                        int count = dt.Columns.Count;
                        for(int i=0;i<dt.Rows.Count;i++)
                        {
                              dt.Rows[i][count-1]= i+1;
                        }
                                                
                        dgAllAccountConstraints.DataSource = dsOvertureAccounts.Tables[0];
                        
                        Session["SessionDataSetOvAccounts"]= dsOvertureAccounts;
                        
                        DataView ViewOvAccounts = dsOvertureAccounts.Tables[0].DefaultView;
                        Session["SessionDataViewOvAccounts"]= ViewOvAccounts;
                        //dgAccounts.DataSource = dsOverture;
                        //dgAccounts.DataMember = "AccountOverview";
                        dgAllAccountConstraints.DataBind();


And in the Update Command of the datagrid..I am doing this...


private void dgAllAccountConstraints_UpdateCommand(object source, DataGridCommandEventArgs e)
            {
                  DataSet dsOvertureAccounts = (DataSet)Session["SessionDataSetOvAccounts"];
                  SqlCommand cmdUpdate;
                  
                  //dsOvertureAccounts.Tables[0].TableName = "AllAccountConstraints";
                  DataRow[] adrEdit = dsOvertureAccounts.Tables[0].Select("Identity='"+dgAllAccountConstraints.DataKeys[e.Item.ItemIndex] + "'");
                  
                  
                  if(adrEdit.Length > 0)
                  {
                  Label lblEngine = new Label();
                  lblEngine = (Label)(e.Item.Cells[0].FindControl("lblEngine"));
           
                  Label lblAccount = new Label();
                  lblAccount = (Label)(e.Item.Cells[1].FindControl("lblAccount"));
                  adrEdit[0]["Account"] = lblAccount.Text;
                      
                  Label lblConstraint = new Label();
                  lblConstraint = (Label)(e.Item.Cells[2].FindControl("lblConstraint"));
                  adrEdit[0]["Constraint"] = lblConstraint.Text;

                  TextBox txtValue = (TextBox) e.Item.Cells[3].Controls[0];
                  adrEdit[0]["Value"] = txtValue.Text;            

                                    
                  if(lblEngine.Text.ToString() =="Overture")
                        cmdUpdate = new SqlCommand("dbo.sp_AgentUIUpdateAccountConstraints", GetConnection("Overture").Connection);
                  else
                        cmdUpdate = new SqlCommand("dbo.sp_AgentUIUpdateAccountConstraints", GetConnection("Google").Connection);

                  cmdUpdate.CommandType = CommandType.StoredProcedure;

                  cmdUpdate.Parameters.Add("@Account", SqlDbType.VarChar,255,"Acccount");
                  //cmdUpdate.Parameters["@Account"].Value = lblAccount.Text;
                  
                  cmdUpdate.Parameters.Add("@Constraint",SqlDbType.VarChar,255,"Constraint");
                  //cmdUpdate.Parameters["@Constraint"].Value = lblConstraint.Text;


                  cmdUpdate.Parameters.Add("@Value",SqlDbType.Float, 8,"Value");
                  //cmdUpdate.Parameters["@Value"].Value = txtValue.Text;
            
                  adapter = new SqlDataAdapter();

                  adapter.UpdateCommand = cmdUpdate;

                        try

                        {
                              dsOvertureAccounts.Tables["AllAccountConstraints"].TableName = "AllAccountConstraints";
                              dsOvertureAccounts.AcceptChanges();
                              
                              adapter.Update(dsOvertureAccounts, "AllAccountConstraints");
                              
                              dgAllAccountConstraints.EditItemIndex = -1;

                              //dgAllAccountConstraints.DataSource = dsOvertureAccounts;                  
                              DataView ViewOvAccounts = dsOvertureAccounts.Tables["AllAccountConstraints"].DefaultView;
                              Session["SessionDataViewOvAccounts"]= ViewOvAccounts;
                              dgAllAccountConstraints.DataSource = ViewOvAccounts;
                              dgAllAccountConstraints.DataBind();
                              
                              
                        }
//                        catch (DBConcurrencyException ex)
//                        {
//                              string customErrorMessage;
//                              customErrorMessage = "Concurrency violation\n";
//                              customErrorMessage += ex.ToString();                  
//                        }
                        catch (Exception ex)
                        {
                              throw new Exception("Unable to update account constraints", ex);
                        }
                                                
                  }


            
            }


It doesnt update the database. Can you please tell me what could be wrong??

Thanks.


0
 
LVL 28

Accepted Solution

by:
mmarinov earned 125 total points
ID: 12395575
Hi jackrock79,

have you tried to just call cmdUpdate.ExecuteNonQuery() method BUT not adapter.Update() method ?
it must be executed

Regards!
B..M
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12438779
Hi jackrock79,

can you explain the B Grade?
Please read this : http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/help.jsp#hi73

B..M
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
crm development 2 67
ASP.NET e-commerce website 4 57
error on the asp.net page 1 33
How to make a GridView cell hyperlinked using C# ? 3 14
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now