|
I have been looking all over for an answer to this question and can't find something that fits exactly what I am trying to do.
I have a DataGridView that is bound to a DataTable, which is part of a strongly typed DataSet from a SQL Server DB. I can manually update the DataGridView fine. However, my program has multiple threads, one of which adds and removes rows of the underlying data in the DataTable to which the DataGridView is bound. This itself works ok sometimes, but at other times I receive the following exception:
Exception in DataGridView: System.IndexOutOfRangeException: Index 9 does not have a value at System.Windows.Forms.CurrencyManager.get_Item(Int32 index) at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)
To replaces this default dialog please handle the DataError event.
I have written test code and have confirmed that the problem seems to arise because the DataTable is being changed on a different thread. When I add/remove rows on the GUI thread, all is fine. But when I add/remove rows from a different thread, I get the Exception.
I have tried to use the Delete method rather than the Remove method in the DataTable class, but that does not seem to make a difference. And for purposes of my program, it is highly undesirable to change the structure of the program so that the GUI thread makes the changes to the DataTable. I suppose an option might be to catch the DataError exception and do nothing with it, but that is really a last resort. Please help here!
|
|
|
|
|
Hi,
1. please don't apply PRE tags to text. PRE tags is for special formatting, it applies to code snippets, and tabular data, not regular text.
2. you shouldn't read and write data structures in general from more than one thread without proper locks. Adding/removing a row in a DT or a DGV may (and probably always will) render the internal state invalid for as long as the update takes, so another thread reading it would get confused, obtain wrong data, and maybe even hang. I see basically three ways to resolve your problem:
a) use real locks; as one of the interested parties is the main thread (which displays the DGV for you), this is a no-no, as you shouldn't keep the main thread waiting at all.
b) tell the other thread to have the updates delegated to the main thread; there are at least two ways to get that done:
b1) with Control.Invoke, see this little article[^];
b2) with BackgroundWorker.ReportProgress, which probably is most appropriate for your situation.
c) use two DataTables: one that gets displayed (by the main thread), and one that gets calculated (by the background thread); when the calculation finishes, have the main thread display the new DT rather than the old one. "ping-pong".
Luc Pattyn [My Articles] Nil Volentibus Arduum
modified on Thursday, September 15, 2011 10:52 PM
|
|
|
|
|
Hi All, I need to retrive value of GridView slelected row cell value.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowSorting="True" >
<Columns>
<asp:BoundField HeaderText="First" DataField="RecNo"/>
<asp:BoundField HeaderText="Second" DataField="DecFN"/>
<asp:BoundField HeaderText="Third" DataField="DecLN"/>
<asp:BoundField HeaderText="Fourth" DataField="DOC"/>
<asp:BoundField HeaderText="Fifth" DataField="UserID"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" Text="Edit" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" CausesValidation="false" Text="Print" OnClick="Butoon2_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#DDFFEE" />
</asp:GridView>
If I click on button1 or Button2 I want to retrive value of a specefic cell value
thanks,
|
|
|
|
|
Add a CommandName property to your buttons like this.
<asp:Button ID="Button1" runat="server" CausesValidation="false" Text="Edit" OnClick="Button1_Click" CommandName="Button1Command"/>
And then handle the RowCommand event. See below
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Button1Command")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
}
}
Signature construction in progress. Sorry for the inconvenience.
|
|
|
|
|
hi,
I have below code. how do I replace with regular expression or much sipler to this.?
many thanks
if(!identier.StartsWith("L#")
identifier.Replace(something,"");
if(!identier.StartsWith("X#")
identifier.Replace(something,"");
if(!identier.StartsWith("G#")
identifier.Replace(something,"");
|
|
|
|
|
Looking at your example, the "pattern" that "jumps out at me" is that in each case you are using the first two characters of (what I assume is the string) 'identifier.'
That suggests, to me, a switch/case statement using the first two characters of the 'identifier' string as the 'match' criterion.
But, if your 'heart is set' on a RegEx solution, don't let me deter you
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
it was my mistake..yes...identifier is a string type.
|
|
|
|
|
You can't - your code is wrong, on a number of levels.
Firstly:
if(!identier.StartsWith("L#")
identifier.Replace(something,""); Is incomplete - the if condition is never terminated (you need a ')' in there.
(Assuming you fix that) Secondly:
If identifier starts with "L#" then it won't start with "X#". So, your replace operation will be called by at least one of the first two if conditions, and then always by the third.
(Assuming you fix that) Thirdly:
Strings in .NET are immutable - they cannot be changed - so string.Replace does nothing at all to identifier, but instead generates a new string, which you then discard.
As a result, your entire code block as shown can be replaced by nothing, as it has no effect whatsoever on subsequent code! You don't need a regex for that!
Think again what you are trying to do, and perhaps explain to us what results you are expecting, becauae that is not going to give them to you.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Certainly your first point and conclusion are correct, but the other points assume a String , which may not be a safe assumption. identifier may be a StringBuilder (for instance*) and the StartsWith method may be an Extension Method -- we just don't know, not enough information was provided.
If in fact your third point is incorrect, and identifier is mutable, then your second point is also not valid.
Having said that, I wouldn't bet against your being absolutely correct on all points.
* It could also be some other unknown class.
|
|
|
|
|
I agree with you completely - it's just Occam suggests my answer, given the code quality of the whole fragment
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
+5 for bringing in my favorite "razor," William of Ockham, as well as, of course, a good, solid, technical answer.
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
+5 for acute analysis thoroughly applied ! best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
my requirement is if a string starts with "L#",or "G#" or "X#" i dont want to replace some part of the string. Otherwise I will replace.
for example: string identifier="L#1234";
In this case I dont want to replace any thing there. Otherewise I would replace some part of the string there.
|
|
|
|
|
Then why worry about using a regular expression? There's no need to do this - sometimes it's just easier to directly manipulate a string - in this case, if the second character isn't hash then you don't need to check the first character.
|
|
|
|
|
got it..thanks. sometimes we think too much for simple ones.
|
|
|
|
|
It depends on the declaration of identifier. Assuming it is a string, here is my shortest replacement:
new System.Text.RegularExpressions.Regex(identifier.ToString());
Others already explained why it does the same your code does.
And yes, the ToString() part is necessary.
|
|
|
|
|
Luc Pattyn wrote: And yes, the ToString() part is necessary.
Ummm... what?
|
|
|
|
|
Exactly what I said.
|
|
|
|
|
I have a partial class that extends my property class, which is a table provided by the entity framework. I am extending that class to supply a string that contains a full address from address parts (number, street name, etc...)
It looks like this, in brief:
public partial class Property
{
public string fullAddress
{
get
{
string address = this.number.ToString();
if (!string.IsNullOrEmpty(this.direction))
{
address += " " + this.direction;
}
return address;
}
}
}
When I type this. Intellisense gives me the option to use direction or _direction. What is the difference and which should I use for getting?
I know I have read this somewhere before but it has slipped my mind.
Cheers, --EA
|
|
|
|
|
Where and how is the variable (or is it a Property ?) 'direction' defined ? Is '_direction' defined anywhere in your code ? best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
It is not defined anywhere in my code. I assume it is an auto generated item from the edmx, but I don't know for certain.
Cheers, --EA
|
|
|
|
|
I was asking about 'direction' and '_direction' used in your code. I'm a little confused by this thread's title which speaks of 'number' and '_number.'
The immediate 'hypothesis' is that '_direction' is a private backing field for a Property somewhere named 'direction;' but, if that's true, how does it get exposed at this point in your code ?
You might clarify in this code example what object 'this' is a reference to ?
Are you dealing with 'black box' code here you can't use the Object Browser in VS Studio on ?
Wish I could help, but my psychic powers are dimmer every day.
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
Fading psychic powers are one of the biggest issues facing our generation today.
Still, in regards to your questions:
1. "this" is in reference to the property class which this is an extension of. It extends the property class from entity framework and is thus related to a sql table which contains address information.
2. I didn't really think that one thing that was interchangable with another required definition. For each attribute of the class there is a matching _field. And now that you mention it I believe you are right that they are autogenerated private backing fields.
public partial class Property
{
public string fullAddress
{
get { return GetFullAddress(this); }
}
private string GetFullAddress(Property property)
{
string tempAddress = property.number.ToString() + ' ';
if (!String.IsNullOrEmpty(property.direction))
tempAddress += property.direction + ' ';
tempAddress += property.street + ' ';
if (!String.IsNullOrEmpty(property.StreetSuffix.name))
tempAddress += property.StreetSuffix.name;
return tempAddress;
}
}
Truth be told this whole thing was more of a curiosity than a question that required anyone tapping in to their well of psychic (and slightly condescending) well.
Thanks for your help, --EA
|
|
|
|
|
Eddie, I apologize if anything in my responses seemed condescending ... I certainly did not have that intent; I hoped you, and any other reader, would experience the 'psychic powers' comment as funny.
As for "our generation:" I bet that I am old ... in years ... enough to be your father
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
That is more than possible, though I do have the hairline and male pattern baldness of a sixty year old man.
Still, the internet has a funny way of making things come across as cynical. Thanks for your response, if I had just remembered the term private backing field it would have helped. So apparently I have the memory of a sixty year old man as well.
Cheers, --EA
|
|
|
|