Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hope any can help in me this,
i have a datagridview and and tabcontrol,
tabcontrol has three tab pages,
tabPage_add,tabPage_edit and tabPage_view,
tabPage_add for adding the records,
tabPage_edit for editing and updating,
tabPage_view for Viewing,
in tabPage_view i have datagridview as 'dataGridView1'
i am bounding that gridView Data in a function called SetDataGridViewData()
i have added the Columns of dataGridView1 and have put their DisplayPropertyName to the Corresponding Columns of Database Table,
now
my need is that, if i want to edit the particular record in dataGridView1 then first the row of dataGridView1 must be selected and then can be navigated to tabPage_edit, the tabControl1_Selected Event Handler is like this
private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPage.Text.ToLower() == "add")
            {
                e.TabPage.Controls.Clear();
                setForAdd();
                e.TabPage.Controls.Add(tableLayoutPanel1);

            }
            else if (e.TabPage.Text.ToLower() == "edit")
            {
                e.TabPage.Controls.Clear();
                setForEdit();
                e.TabPage.Controls.Add(tableLayoutPanel1);
            }
            else
            {
                setDataGridViewData();
            }

        }


where setForEdit() is like this
void setForEdit()
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                btn_add.Visible = false;
                btn_delete.Visible = true;
                btn_update.Visible = true;
                lbl_subhead.Text = "Edit Available Drug";

                setPanelById(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            }// this line giving error as Object Reference is not set to the instance of Object
            else
                tabControl1.SelectedIndex = 2;
        }


the code for setForAdd() is like this
void setForAdd()
      {
          btn_add.Visible = true;
          btn_delete.Visible = false;
          btn_update.Visible = false;
          lbl_subhead.Text = "Add Available Drug";
          setEmptyScreenFields();
          setId();
      }



the code of dataGridView1 in the designer.cs is as
//
           // dataGridView1
           //
           this.dataGridView1.AllowUserToAddRows = false;
           this.dataGridView1.AllowUserToDeleteRows = false;
           this.dataGridView1.Anchor = System.Windows.Forms.AnchorStyles.Top;
           this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
           this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
           this.Column_id,
           this.Column_name,
           this.Column_type,
           this.Column_mnfrid,
           this.Column_expiryDate});
           this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;
           this.dataGridView1.Location = new System.Drawing.Point(150, 166);
           this.dataGridView1.Name = "dataGridView1";
           this.dataGridView1.ReadOnly = true;
           this.dataGridView1.Size = new System.Drawing.Size(627, 340);
           this.dataGridView1.TabIndex = 2;
           this.dataGridView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseDown);




the remaining are doing exactly needed operations



thanx if somebody can help me.......
Posted
Updated 26-Jul-10 23:27pm
v3

1 solution

sorry techies and expertises,
i was doing something wrong...
i had given the DataPropertyName of 'Column_mnfrId' as 'ad_mnfrId' but it is not the Column Name in DataBase Table... the Correct one is "ad_manufacturerId' ......
Due to this i was getting an extra Column in DataGridView which made the Index of DataGridViewColumn changed ...
totally i have 5 Columns but i was getting 6 Columns with one Starting Column as Null text( the Column whose DataPropertyName i gave incorrectly, thought this column was displaying at the Correct Index, but it was getting the Index of '0' at Runtime so i was getting the Error....

Column_mnfrId' Index i had given as '3' means fourth column..
even thought it was displaying at the fourth position , the value
that was returning by the line


dataGridView1.SelectedRows[0].Cells[0].Value.ToString()
was that Column Cell Value...(Null Value)...

But where as i had given the Column at Index '0' as another Column...


i would like to share something what i think is that....
when you give a wrong "DataProperyName" to a DataGridViewColumn then it will get the Index as '0' even though You assign your Columns in the Predefined Indices....

if somebody has any idea or some stuff to or share..... i would be glad..

i may be wrong but i would be happy if somebody corrects me...

thank you so much frnds......
 
Share this answer
 
v3
Comments
Dalek Dave 27-Jul-10 7:00am    
Reason for my vote of 5
To admit openly you made the mistake and show where is helpful.
koool.kabeer 27-Jul-10 7:37am    
yes i made mistake.... i admit it.... but may this be helpful for somebody...... thanx for your review ......
koool.kabeer 27-Jul-10 7:40am    
But there is something to notice here......
dataGridView1.SelectedRows[0].Cells[0].Value
is the Value of the Null Value Column(the one whose DisplayPropertyName was wrongly given.......
Hope anybody would like to share something regarding this....

thanx thanx thanx alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900