How to get Devexpress XtraGrid control selected row












0















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;
}
}









share|improve this question

























  • 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 a form_Shown event instad of form_Load event

    – Jens Kloster
    Apr 3 '13 at 11:42
















0















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;
}
}









share|improve this question

























  • 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 a form_Shown event instad of form_Load event

    – Jens Kloster
    Apr 3 '13 at 11:42














0












0








0








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;
}
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 3 '13 at 9:35







aby

















asked Apr 3 '13 at 8:46









abyaby

41561934




41561934













  • 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 a form_Shown event instad of form_Load event

    – Jens Kloster
    Apr 3 '13 at 11:42



















  • 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 a form_Shown event instad of form_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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer


























  • @aby is this answer working for you?

    – Jens Kloster
    May 6 '13 at 10:52











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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer


























  • @aby is this answer working for you?

    – Jens Kloster
    May 6 '13 at 10:52
















0














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.






share|improve this answer


























  • @aby is this answer working for you?

    – Jens Kloster
    May 6 '13 at 10:52














0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • @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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values