| |

VerySource

 Forgot password?
 Register
Search
View: 1070|Reply: 9

About batch update of DataGrid data in asp.net

[Copy link]

3

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-24 02:00:01
| Show all posts |Read mode
In asp.net, I use DataSet to get it. Bind DataSet.Tables ["***"]. DefaultView in the DataGrid. Now that I have modified some data in the DataGrid, how can I update the data in batches? Take out all the rows in the DataGrid and use update Update?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-10 12:45:01
| Show all posts
reference:

/// <summary>
 /// Summary description of WebForm2.
 /// </ summary>
 public class WebForm2: System.Web.UI.Page
 {
  private string strcon = "server = localhost; database = pubs; uid = sa; pwd = wang";
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
 
  private void Page_Load (object sender, System.EventArgs e)
  {
   if (! IsPostBack)
   {
    BindGrid ();
   }
  }
  private void BindGrid ()
  {
   System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection (strcon);
   string sql = "select top 5 * from authors order by au_id desc";
   System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand (sql, con);
   con.Open ();
   DataGrid1.DataSource = cmd.ExecuteReader ();
   DataGrid1.DataBind ();
   con.Close ();
  }

  #region Web Form Designer Generated Code
  override protected void OnInit (EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Forms designer.
   //
   InitializeComponent ();
   base.OnInit (e);
  }
  
  /// <summary>
  /// Designer supports required methods-don't use code editor to modify
  /// The contents of this method.
  /// </ summary>
  private void InitializeComponent ()
  {
   this.DataGrid1.ItemCommand + = new System.Web.UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_ItemCommand);
   this.DataGrid1.ItemDataBound + = new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_ItemDataBound);
   this.Load + = new System.EventHandler (this.Page_Load);

  }
  #endregion

  private void DataGrid1_ItemCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   if (e.Item.ItemType == ListItemType.Footer)
   {
    if (e.CommandName == "edit")
    {
     for (int i = 0; i <DataGrid1.Items.Count; i ++)
     {
      
      TextBox t1 = new TextBox ();
      t1 = (TextBox) DataGrid1.Items [i] .FindControl ("TextBox1");
     
      TextBox t2 = new TextBox ();
      t2 = (TextBox) DataGrid1.Items [i] .FindControl ("TextBox2");
      string id = DataGrid1.Items [i] .Cells [0] .Text;
      string sql = "update authors set au_fname = '" + t1.Text + "', au_lname = '" + t2.Text + "' where au_id = '" + id + "'";
      if (Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery (strcon, CommandType.Text, sql)! = 1)
      {
       Response.Write ("Update failed!");
       return;
      }
      
      //Response.Write("<Li>ID:"+DataGrid1.Items[i].Cells[0].Text);
      //Response.Write("Update content 1: "+ t1.Text);
      //Response.Write("Update content 2: "+ t2.Text);
     
     }

     BindGrid ();
    }
   }
  }

  private void DataGrid1_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   //if(e.Item.ItemType == ListItemType.Footer)
   // {
    // Button b1 = (Button) e.Item.FindControl ("Button1");
    //b1.Attributes.Add("onclick","return confirm ('Do you really want to update all?'); ");
   //}

   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    TextBox t1, t2;
    t1 = (TextBox) e.Item.FindControl ("TextBox1");
    t1.Attributes.Add ("onfocus", "this.className = 'edit'");
    t1.Attributes.Add ("onblur", "this.className = 'noedit'");

    t2 = (TextBox) e.Item.FindControl ("TextBox2");
    t2.Attributes.Add ("onfocus", "this.className = 'edit'");
    t2.Attributes.Add ("onblur", "this.className = 'noedit'");

   }
  }




 }
HTML:

<! DOCTYPE HTML PUBLIC "-// W3C // DTD HTML 4.0 Transitional // EN">
<HTML>
 <HEAD>
  <title> WebForm2 </ title>
  <meta content = "Microsoft Visual Studio .NET 7.1" name = "GENERATOR">
  <meta content = "C #" name = "CODE_LANGUAGE">
  <meta content = "JavaScript" name = "vs_defaultClientScript">
  <meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetSchema">
  <LINK href = "1.css" type = "text / css" rel = "stylesheet">
 </ HEAD>
 <body>
  <form id = "Form1" method = "post" runat = "server">
   <asp: datagrid id = "DataGrid1" runat = "server" CellPadding = "3" BackColor = "White" BorderWidth = "1px"
    BorderStyle = "None" BorderColor = "# CCCCCC" AutoGenerateColumns = "False" ShowFooter = "True" ShowHeader = "False">
    <FooterStyle ForeColor = "# 000066" BackColor = "White"> </ FooterStyle>
    <SelectedItemStyle Font-Bold = "True" ForeColor = "White" BackColor = "# 669999"> </ SelectedItemStyle>
    <ItemStyle ForeColor = "# 000066"> </ ItemStyle>
    <HeaderStyle Font-Bold = "True" ForeColor = "White" BackColor = "# 006699"> </ HeaderStyle>
    <Columns>
     <asp: BoundColumn DataField = "au_id" HeaderText = "ID"> </ asp: BoundColumn>
     <asp: TemplateColumn>
      <ItemTemplate>
       <asp: TextBox id = TextBox1 runat = "server" Text = '<% # DataBinder.Eval (Container.DataItem, "au_fname")%>' CssClass = "noedit">
       </ asp: TextBox>
       <asp: RequiredFieldValidator id = "RequiredFieldValidator1" runat = "server" ErrorMessage = "*!" ControlToValidate = "TextBox1"
        EnableViewState = "False"> </ asp: RequiredFieldValidator>
      </ ItemTemplate>
     </ asp: TemplateColumn>
     <asp: TemplateColumn>
      <ItemTemplate>
       <asp: TextBox id = TextBox2 runat = "server" Text = '<% # DataBinder.Eval (Container.DataItem, "au_lname")%>' CssClass = "noedit">
       </ asp: TextBox>
       <asp: RequiredFieldValidator id = "RequiredFieldValidator2" runat = "server" ErrorMessage = "*!" ControlToValidate = "TextBox2"
        EnableViewState = "False"> </ asp: RequiredFieldValidator>
      </ ItemTemplate>
      <FooterTemplate>
       <DIV align = "right">
        <asp: Button id = "Button1" runat = "server" Text = "Update" CommandName = "edit"> </ asp: Button> <INPUT type = "reset" value = "Restore">&nbsp; </ DIV>
      </ FooterTemplate>
     </ asp: TemplateColumn>
    </ Columns>
    <PagerStyle HorizontalAlign = "Left" ForeColor = "# 000066" BackColor = "White" Mode = "NumericPages"> </ PagerStyle>
   </ asp: datagrid>
  </ form>
 </ body>
</ HTML>
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-11 12:15:02
| Show all posts
mark
Reply

Use magic Report

0

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Italy

Post time: 2020-2-13 20:30:01
| Show all posts
Top
Reply

Use magic Report

3

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 United States

 Author| Post time: 2020-4-11 16:45:01
| Show all posts
thestarwar
Is it just to take out the contents of the table with a loop and then update it, is there any other way
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Unknown

Post time: 2020-4-16 15:30:01
| Show all posts
It should be possible to put the data in the dataadapter, and then use the update method of sqladapter to update
But I have n’t used it, and it feels not practical. It has been traversing the entire datagrid to update
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 India

Post time: 2020-4-25 06:15:01
| Show all posts
There should be a better way, which is too complicated!
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-4-26 08:00:02
| Show all posts
if you retrive your data from database and store it in dataset

---- get the selectedID, note that the following code only change the data
     in your dataset rather than database
form each row in dataset.table ("table"). rows
  if row.item ("id") = selectedID
  row.delete
end if
next

--------- when you click the "submit" botton

dim rowafftected as integer

rowaffected = dataadapter.update (dataset ', "yourTable")
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-30 07:15:01
| Show all posts
I also don't quite understand it.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 10:15:01
| Show all posts
Not bad!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list