Click here to Skip to main content
15,899,126 members
Home / Discussions / C#
   

C#

 
GeneralRe: Error while insert DB Pin
Ronen Kfir8-Jul-05 20:28
Ronen Kfir8-Jul-05 20:28 
GeneralRe: Error while insert DB Pin
Guffa9-Jul-05 7:04
Guffa9-Jul-05 7:04 
GeneralRe: Error while insert DB Pin
sea#9-Jul-05 10:29
sea#9-Jul-05 10:29 
GeneralRe: Error while insert DB Pin
Rob Graham8-Jul-05 15:10
Rob Graham8-Jul-05 15:10 
GeneralEdit controls properties in other thead Pin
machocr8-Jul-05 10:30
machocr8-Jul-05 10:30 
GeneralRe: Edit controls properties in other thead Pin
Rob Graham8-Jul-05 15:32
Rob Graham8-Jul-05 15:32 
GeneralDataGrid---UpdateCommand... problem... Pin
just4ulove78-Jul-05 10:15
just4ulove78-Jul-05 10:15 
GeneralRe: DataGrid---UpdateCommand... problem... Pin
Snowblind379-Jul-05 7:30
Snowblind379-Jul-05 7:30 
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.

<br />
private void updateButton_Click(object sender, System.EventArgs e)<br />
{<br />
//DataGridTableStyle provides the mechanisms<br />
DataGridTableStyle ts1 = new DataGridTableStyle();<br />
<br />
//DataGridCell allows you to hold the contents of the specified cell<br />
DataGridCell myCell;<br />
myCell = UserdataGrid.CurrentCell;<br />
<br />
// map the DataGridTableStyle object to the database source <br />
ts1.MappingName = "SessionTable";<br />
<br />
//Add the DataGridTableStyle object to the grid's TableStyles collection<br />
UserdataGrid.TableStyles.Add(ts1);<br />
<br />
string infoStr = null;<br />
int j = 0;<br />
<br />
//The two foreach statements is a loop within a loop<br />
//The outer loop deals with the row index<br />
//The inner loop returns the column number (j) if there is a match<br />
//and the infoStr(column to change)<br />
foreach(DataGridTableStyle gridStyle in UserdataGrid.TableStyles)<br />
{<br />
<br />
foreach(DataGridColumnStyle colStyle in gridStyle.GridColumnStyles)<br />
{<br />
if (j == myCell.ColumnNumber)<br />
{<br />
infoStr = colStyle.MappingName;<br />
}<br />
j++;<br />
}<br />
//end inner foreach<br />
}<br />
//end outer foreach<br />
<br />
//The element selection has been found <br />
//and the current row index is where it can be found	<br />
int i = UserdataGrid.CurrentRowIndex;<br />
<br />
//Get the unique id associated with this row<br />
//Note that I needed to make this GUID id part of <br />
//my DataTable so that when I update the sql table<br />
//I am updating only that particular record<br />
//and not other records that may have similiar entries<br />
String userId = UserdataGrid[i, 7].ToString();<br />
<br />
//The UpdateRecord method creates an SQL update statement<br />
//Parameters are:<br />
//Table to change<br />
//Column to change<br />
//Column to search for GUID id<br />
//match on GUID id will determine row index<br />
//Value to update<br />
sql.UpdateRecord("SessionTable", infoStr, "UserId", userId, UserdataGrid[myCell].ToString());<br />
<br />
//Now all we need to do is show the changes in the DataGrid<br />
FillData();<br />
UserdataGrid.SetDataBinding(dataSet, "SessionTable");<br />
UserdataGrid.Refresh();<br />
}<br />
<br />


Whoooo!!! It sure seems like alot of work just to get a value, change it and then display the changes. But there are some positive things to note:

1) This is the entire implementation(minus the sql statements for the database and the implementation of FillData())
2) I did not have to use EditCommand
3) Though not the most elegent solution, it works!

Well, I hope this helps. I know what it's like to wonder why something isn't working when it looks like it should. Sometimes we need to look outside of the "logical box" to make it work.............

Have a good one,


Snowblind37 aliasing as CodeBlind37 these days.
GeneralList definition in C# Pin
zaboboa8-Jul-05 9:07
zaboboa8-Jul-05 9:07 
GeneralRe: List definition in C# Pin
Stefan Troschuetz8-Jul-05 9:38
Stefan Troschuetz8-Jul-05 9:38 
GeneralRe: List definition in C# Pin
zaboboa8-Jul-05 9:41
zaboboa8-Jul-05 9:41 
GeneralRe: List definition in C# Pin
Stefan Troschuetz8-Jul-05 10:03
Stefan Troschuetz8-Jul-05 10:03 
GeneralRe: List definition in C# Pin
Snowblind379-Jul-05 8:01
Snowblind379-Jul-05 8:01 
GeneralSearch &amp; replacing values in Xml Pin
breen258-Jul-05 9:05
breen258-Jul-05 9:05 
GeneralMDI child can't close itself Pin
Luis Alonso Ramos8-Jul-05 9:00
Luis Alonso Ramos8-Jul-05 9:00 
GeneralRe: MDI child can't close itself Pin
Snowblind379-Jul-05 8:24
Snowblind379-Jul-05 8:24 
GeneralRe: MDI child can't close itself Pin
Luis Alonso Ramos9-Jul-05 9:54
Luis Alonso Ramos9-Jul-05 9:54 
GeneralRe: MDI child can't close itself Pin
Snowblind379-Jul-05 11:20
Snowblind379-Jul-05 11:20 
Questionbest way to connect to a database? Pin
Jassim Rahma8-Jul-05 7:32
Jassim Rahma8-Jul-05 7:32 
AnswerRe: best way to connect to a database? Pin
breen258-Jul-05 8:47
breen258-Jul-05 8:47 
GeneralSimple Instance Error Msg Pin
zaboboa8-Jul-05 5:32
zaboboa8-Jul-05 5:32 
GeneralRe: Simple Instance Error Msg Pin
Dave Kreskowiak8-Jul-05 5:42
mveDave Kreskowiak8-Jul-05 5:42 
GeneralRe: Simple Instance Error Msg Pin
Stefan Troschuetz8-Jul-05 5:51
Stefan Troschuetz8-Jul-05 5:51 
GeneralRe: Simple Instance Error Msg Pin
zaboboa8-Jul-05 6:14
zaboboa8-Jul-05 6:14 
GeneralRe: Simple Instance Error Msg Pin
Stefan Troschuetz8-Jul-05 9:40
Stefan Troschuetz8-Jul-05 9:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.