Windows Develop Bookmark and Share   
 index > Windows Forms Designer > how to diasble cells in datagrid view
 

how to diasble cells in datagrid view

hi all

I ve datagrid view dginvoicelist which contains

Id,date,amount,note,transaction

upon clicking a button addinvoice , a widows form appears where in we insert the detials above ,once saved all the details are put into datagrid mentioned above .

My problem is if user clicks on any of the cells in gridview it displays this error in this event

private void dgInvoiceList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{
if (dgInvoiceList.SelectedRows[0].Cells["TransactionComplete"].Value.Equals(false)) //Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

}

why is this?how to solve?
Can i disable the cells ,as when addinvoice button is clicked windows form appears ,for edit also windows form appears,upon saving the values sit in datagridview

So can I disable the cells so that if user clicks on cells this error wont appear

If i disable willl i be able to edit or add when I click buttons??

meghanalohit  Thursday, February 05, 2009 4:57 AM

Hi meghanalohit,

You need to put the code in the constructor method or handle the Load event of the form.

publicForm1()
{
InitializeComponent();
dgInvoiceList.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
}

OR

privatevoidForm1_Load(objectsender,EventArgse)
{
dgInvoiceList.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
}

Please test it and tell me if you meet any question.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer bymeghanalohit Tuesday, February 10, 2009 2:59 AM
  •  
Kira Qian  Monday, February 09, 2009 2:22 AM

Hi meghanalohit,

Once you use SelectedRows[index], you should first set the "SelectionMode" of DataGridView to "FullRowSelect".

dataGridView1.SelectionMode=DataGridViewSelectionMode.FullRowSelect;


Otherwise you will get the exception.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, February 06, 2009 9:50 AM
hey
its throwin same exception again
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I did put ur suggested code still it throws same exception
I put the code in 2 places but it throws the error :(

private void dgInvoiceList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)

{

//check that the header is not clicked on

if (e.RowIndex >= 0)

{

dgInvoiceList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

try

{

if (dgInvoiceList.SelectedRows[0].Cells["TranscationComplete"].Value.Equals(false))

{

dgInvoiceList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

btnEditInvoice.Enabled = true;

btnDeleteInvoice.Enabled = true;

txtDiscount.Enabled = false;

txtTax.Enabled = false;

txtTotal.Enabled = false;

btnComputeTotal.Enabled = false;

groupBox2.Enabled = false;

btnAddNewDetail.Enabled = false;

btnDeleteInvoiceDetails.Enabled = false;

btnsave.Enabled = false;

dgInvoiceList.Rows[e.RowIndex].Selected = true;

}

else

{

txtTotal.Enabled = false;

btnsave.Enabled = false;

btnEditInvoice.Enabled = false;

btnDeleteInvoice.Enabled = false;

}

}

catch (Exception ex)

{

throw (ex);

}

}

}

meghanalohit  Friday, February 06, 2009 11:27 AM

Hi meghanalohit,

You need to put the code in the constructor method or handle the Load event of the form.

publicForm1()
{
InitializeComponent();
dgInvoiceList.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
}

OR

privatevoidForm1_Load(objectsender,EventArgse)
{
dgInvoiceList.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
}

Please test it and tell me if you meet any question.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer bymeghanalohit Tuesday, February 10, 2009 2:59 AM
  •  
Kira Qian  Monday, February 09, 2009 2:22 AM
Hey it worked .thanks:)
meghanalohit  Tuesday, February 10, 2009 2:59 AM

You can use google to search for other answers

Custom Search

More Threads

• Horizontal Scroll Bars
• design mode and copy & paste
• Event handling in design time mode
• AutoComplete ComboBox with DynamicItemsList
• Control.Setbounds Intellisense bug
• Designer Capable ImageList that uses the Resources...
• Localization of a control is not firing
• Forms inheritance CreateInstanceImpl
• Code snippets in toolbox
• Ann: SharpLibrary Released