How to get Devexpress XtraGrid control selected row
I've a devexpress XtraGrid Control. But, I couldn't get the ID of a by default selected row when the winform loads. I know how to get it when the user clicks on the grid.
Here is the code snapshot:
private void Form1_Load(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID"));
XtraMessageBox.Show(ID.ToString());
}
public BindingSource bindData(object obj)
{
BindingSource ctBinding;
try
{
ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
devexpress xtragrid
|
show 2 more comments
I've a devexpress XtraGrid Control. But, I couldn't get the ID of a by default selected row when the winform loads. I know how to get it when the user clicks on the grid.
Here is the code snapshot:
private void Form1_Load(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID"));
XtraMessageBox.Show(ID.ToString());
}
public BindingSource bindData(object obj)
{
BindingSource ctBinding;
try
{
ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
devexpress xtragrid
What is the type inTable1
?
– Jens Kloster
Apr 3 '13 at 9:43
it's a table object that is accessed using LINQ to SQL
– aby
Apr 3 '13 at 11:06
Could you specify the problem you are having. I don't think I understand what you need.
– Jens Kloster
Apr 3 '13 at 11:20
The problem is that every time the form loads it throws "Object reference not set to an instance of an object." When i look deeper into the code, the problem lies on getting the focused row (by default) value. It returns 0 (for the ID) even though there are too many rows in the list. It works fine when i click on the rows though. But, what I want it to get the ID when the form loads
– aby
Apr 3 '13 at 11:37
you should really add that to your question. If it returns 0, you are looking at theRowHandle
, not the row value. Try moving your code to aform_Shown
event instad ofform_Load
event
– Jens Kloster
Apr 3 '13 at 11:42
|
show 2 more comments
I've a devexpress XtraGrid Control. But, I couldn't get the ID of a by default selected row when the winform loads. I know how to get it when the user clicks on the grid.
Here is the code snapshot:
private void Form1_Load(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID"));
XtraMessageBox.Show(ID.ToString());
}
public BindingSource bindData(object obj)
{
BindingSource ctBinding;
try
{
ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
devexpress xtragrid
I've a devexpress XtraGrid Control. But, I couldn't get the ID of a by default selected row when the winform loads. I know how to get it when the user clicks on the grid.
Here is the code snapshot:
private void Form1_Load(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID"));
XtraMessageBox.Show(ID.ToString());
}
public BindingSource bindData(object obj)
{
BindingSource ctBinding;
try
{
ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
devexpress xtragrid
devexpress xtragrid
edited Apr 3 '13 at 9:35
aby
asked Apr 3 '13 at 8:46
abyaby
41561934
41561934
What is the type inTable1
?
– Jens Kloster
Apr 3 '13 at 9:43
it's a table object that is accessed using LINQ to SQL
– aby
Apr 3 '13 at 11:06
Could you specify the problem you are having. I don't think I understand what you need.
– Jens Kloster
Apr 3 '13 at 11:20
The problem is that every time the form loads it throws "Object reference not set to an instance of an object." When i look deeper into the code, the problem lies on getting the focused row (by default) value. It returns 0 (for the ID) even though there are too many rows in the list. It works fine when i click on the rows though. But, what I want it to get the ID when the form loads
– aby
Apr 3 '13 at 11:37
you should really add that to your question. If it returns 0, you are looking at theRowHandle
, not the row value. Try moving your code to aform_Shown
event instad ofform_Load
event
– Jens Kloster
Apr 3 '13 at 11:42
|
show 2 more comments
What is the type inTable1
?
– Jens Kloster
Apr 3 '13 at 9:43
it's a table object that is accessed using LINQ to SQL
– aby
Apr 3 '13 at 11:06
Could you specify the problem you are having. I don't think I understand what you need.
– Jens Kloster
Apr 3 '13 at 11:20
The problem is that every time the form loads it throws "Object reference not set to an instance of an object." When i look deeper into the code, the problem lies on getting the focused row (by default) value. It returns 0 (for the ID) even though there are too many rows in the list. It works fine when i click on the rows though. But, what I want it to get the ID when the form loads
– aby
Apr 3 '13 at 11:37
you should really add that to your question. If it returns 0, you are looking at theRowHandle
, not the row value. Try moving your code to aform_Shown
event instad ofform_Load
event
– Jens Kloster
Apr 3 '13 at 11:42
What is the type in
Table1
?– Jens Kloster
Apr 3 '13 at 9:43
What is the type in
Table1
?– Jens Kloster
Apr 3 '13 at 9:43
it's a table object that is accessed using LINQ to SQL
– aby
Apr 3 '13 at 11:06
it's a table object that is accessed using LINQ to SQL
– aby
Apr 3 '13 at 11:06
Could you specify the problem you are having. I don't think I understand what you need.
– Jens Kloster
Apr 3 '13 at 11:20
Could you specify the problem you are having. I don't think I understand what you need.
– Jens Kloster
Apr 3 '13 at 11:20
The problem is that every time the form loads it throws "Object reference not set to an instance of an object." When i look deeper into the code, the problem lies on getting the focused row (by default) value. It returns 0 (for the ID) even though there are too many rows in the list. It works fine when i click on the rows though. But, what I want it to get the ID when the form loads
– aby
Apr 3 '13 at 11:37
The problem is that every time the form loads it throws "Object reference not set to an instance of an object." When i look deeper into the code, the problem lies on getting the focused row (by default) value. It returns 0 (for the ID) even though there are too many rows in the list. It works fine when i click on the rows though. But, what I want it to get the ID when the form loads
– aby
Apr 3 '13 at 11:37
you should really add that to your question. If it returns 0, you are looking at the
RowHandle
, not the row value. Try moving your code to a form_Shown
event instad of form_Load
event– Jens Kloster
Apr 3 '13 at 11:42
you should really add that to your question. If it returns 0, you are looking at the
RowHandle
, not the row value. Try moving your code to a form_Shown
event instad of form_Load
event– Jens Kloster
Apr 3 '13 at 11:42
|
show 2 more comments
1 Answer
1
active
oldest
votes
If I understand you correctly, you need something like this:
private void Form1_Shown(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
var item = gridView.GetFocusedRow() as YourDataType
if(item != null)
{
ID = item.ID;
XtraMessageBox.Show(ID.ToString());
}
}
assuming what your bindData
returns a typed collection of some kind.
** Update **
Moving the code to form_Shown
seemed to do the trick.
@aby is this answer working for you?
– Jens Kloster
May 6 '13 at 10:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15782671%2fhow-to-get-devexpress-xtragrid-control-selected-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand you correctly, you need something like this:
private void Form1_Shown(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
var item = gridView.GetFocusedRow() as YourDataType
if(item != null)
{
ID = item.ID;
XtraMessageBox.Show(ID.ToString());
}
}
assuming what your bindData
returns a typed collection of some kind.
** Update **
Moving the code to form_Shown
seemed to do the trick.
@aby is this answer working for you?
– Jens Kloster
May 6 '13 at 10:52
add a comment |
If I understand you correctly, you need something like this:
private void Form1_Shown(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
var item = gridView.GetFocusedRow() as YourDataType
if(item != null)
{
ID = item.ID;
XtraMessageBox.Show(ID.ToString());
}
}
assuming what your bindData
returns a typed collection of some kind.
** Update **
Moving the code to form_Shown
seemed to do the trick.
@aby is this answer working for you?
– Jens Kloster
May 6 '13 at 10:52
add a comment |
If I understand you correctly, you need something like this:
private void Form1_Shown(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
var item = gridView.GetFocusedRow() as YourDataType
if(item != null)
{
ID = item.ID;
XtraMessageBox.Show(ID.ToString());
}
}
assuming what your bindData
returns a typed collection of some kind.
** Update **
Moving the code to form_Shown
seemed to do the trick.
If I understand you correctly, you need something like this:
private void Form1_Shown(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
var item = gridView.GetFocusedRow() as YourDataType
if(item != null)
{
ID = item.ID;
XtraMessageBox.Show(ID.ToString());
}
}
assuming what your bindData
returns a typed collection of some kind.
** Update **
Moving the code to form_Shown
seemed to do the trick.
edited Apr 3 '13 at 12:33
answered Apr 3 '13 at 8:58
Jens KlosterJens Kloster
8,94243250
8,94243250
@aby is this answer working for you?
– Jens Kloster
May 6 '13 at 10:52
add a comment |
@aby is this answer working for you?
– Jens Kloster
May 6 '13 at 10:52
@aby is this answer working for you?
– Jens Kloster
May 6 '13 at 10:52
@aby is this answer working for you?
– Jens Kloster
May 6 '13 at 10:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15782671%2fhow-to-get-devexpress-xtragrid-control-selected-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the type in
Table1
?– Jens Kloster
Apr 3 '13 at 9:43
it's a table object that is accessed using LINQ to SQL
– aby
Apr 3 '13 at 11:06
Could you specify the problem you are having. I don't think I understand what you need.
– Jens Kloster
Apr 3 '13 at 11:20
The problem is that every time the form loads it throws "Object reference not set to an instance of an object." When i look deeper into the code, the problem lies on getting the focused row (by default) value. It returns 0 (for the ID) even though there are too many rows in the list. It works fine when i click on the rows though. But, what I want it to get the ID when the form loads
– aby
Apr 3 '13 at 11:37
you should really add that to your question. If it returns 0, you are looking at the
RowHandle
, not the row value. Try moving your code to aform_Shown
event instad ofform_Load
event– Jens Kloster
Apr 3 '13 at 11:42