Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. DataGrid---UpdateCommand... problem...

DataGrid---UpdateCommand... problem...

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    just4ulove7
    wrote on last edited by
    #1

    Hi.. I have a datagrid and an EditCommand Now, when I click the UpdateCommand- link button... private void dgProjects_ItemUpdate(object sender,DataGridCommandEventArgs e ) { string ProjectId = e.Item.Cells[0].Text .... } It should return me the first column value in the clicked row??? But its not... Is something wrong.. or some other way...??? Thanks

    S 1 Reply Last reply
    0
    • J just4ulove7

      Hi.. I have a datagrid and an EditCommand Now, when I click the UpdateCommand- link button... private void dgProjects_ItemUpdate(object sender,DataGridCommandEventArgs e ) { string ProjectId = e.Item.Cells[0].Text .... } It should return me the first column value in the clicked row??? But its not... Is something wrong.. or some other way...??? Thanks

      S Offline
      S Offline
      Snowblind37
      wrote on last edited by
      #2

      The answer is a little more complicated than it should be....Or at least I thought it should be. The following code is meant to take the value of the cell (that the user has selected and changed)and write this value into the database. It then refreshes the DataGrid to show the new value. The hardest part to understand is that you have to create a DataGridTableStyle object to do this. The reason for creating this object is that the class provides the necessary methods and properties that you need to access. private void updateButton_Click(object sender, System.EventArgs e) { //DataGridTableStyle provides the mechanisms DataGridTableStyle ts1 = new DataGridTableStyle(); //DataGridCell allows you to hold the contents of the specified cell DataGridCell myCell; myCell = UserdataGrid.CurrentCell; // map the DataGridTableStyle object to the database source ts1.MappingName = "SessionTable"; //Add the DataGridTableStyle object to the grid's TableStyles collection UserdataGrid.TableStyles.Add(ts1); string infoStr = null; int j = 0; //The two foreach statements is a loop within a loop //The outer loop deals with the row index //The inner loop returns the column number (j) if there is a match //and the infoStr(column to change) foreach(DataGridTableStyle gridStyle in UserdataGrid.TableStyles) { foreach(DataGridColumnStyle colStyle in gridStyle.GridColumnStyles) { if (j == myCell.ColumnNumber) { infoStr = colStyle.MappingName; } j++; } //end inner foreach } //end outer foreach //The element selection has been found //and the current row index is where it can be found int i = UserdataGrid.CurrentRowIndex; //Get the unique id associated with this row //Note that I needed to make this GUID id part of //my DataTable so that when I update the sql table //I am updating only that particular record //and not other records that may have similiar entries String userId = UserdataGrid[i, 7].ToString(); //The UpdateRecord method creates an SQL update statement //Parameters are: //Table to change //Column to change //Column to search for GUID id //match on GUID id will determine row index //Value to update sql.UpdateRecord("SessionTable", infoStr, "UserId", userId, UserdataGrid[myCell].ToString()); //Now all we need to do is show the changes in the DataGrid FillData(); UserdataGrid.SetDataBinding(dataSet, "SessionTable"); UserdataGrid.Refresh(); } Whoooo!!! It sure seems like alot of work just to get a value, change it and then display t

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups