4

I Want to save multiple value from window form to datatable and then i bind this table to Datagridview. Value are adding in Datatable but At:

dataGridViewX1.DataSource = dt.DefaultView.Table);

binding point error shows

Object reference not set to an instance of an object

How Can I solve it?

public AddOrder(string ItemName,int Qty,Double Price)
    {


        try
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("Item Name");

            dt.Columns.Add("Qty");
            dt.Columns.Add("Unit Price");
            dt.Columns.Add("Amounts");


            DataRow dr = dt.NewRow();
            dr["id"] = a;
            a++;

            dr["Item Name"] = ItemName;
            dr["Qty"] = Qty;
            dr["Unit Price"] = Price;
            dr["Amounts"] = (Convert.ToInt32(dr["Qty"]) * Convert.ToInt32(dr["Unit Price"]));
            dt.Rows.Add(dr);

            dataGridViewX1.DataSource = dt.DefaultView.Table;

        }
        catch(Exception ee)
        {
            DevComponents.DotNetBar.MessageBoxEx.Show(ee.Message,"Error Message");
        }
    }
Shaharyar
  • 11,393
  • 3
  • 39
  • 59
ziakhan
  • 59
  • 2
  • 4

8 Answers8

1

is the initialization of the dataGridViewX1 in a InitializeComponent method?

    I Think it is parameterized constructor I it does not has initialization of controls

1) solution

   InitializeComponent(); need to call this method.

Its working my application

SURESH MOGUDALA
  • 35
  • 1
  • 12
1

The object reference exception could be occurring in an event handler related to data binding. For example, let's say your datagrid has something like this in the markup:

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound"

When you call GridView1.DataBind(), the GridView1_RowDataBound method executes for each row during the data bind process. This method could contain some bad code to give you the "object reference..." exception.

James Lawruk
  • 26,651
  • 19
  • 117
  • 128
0

Directly assign data table to gridview:

dataGridViewX1.DataSource = dt;

instead of:

dataGridViewX1.DataSource = dt.DefaultView.Table;
Shaharyar
  • 11,393
  • 3
  • 39
  • 59
0

Place a break point and check either you have value in

- dr["Qty"] = Qty;

 - dr["Unit Price"] = Price;

Most probably one of them or both has empty value.

or try this and check

 - dr["Qty"] = 3;

  - dr["Unit Price"] = 4;
shujaat siddiqui
  • 1,297
  • 1
  • 17
  • 37
  • Thanks for reply i this code (dataGridViewX1.DataSource = dt;) but still same problem – ziakhan Jan 01 '14 at 07:12
  • One record is exist in dt, but when i bind it ti datagridviewx1 then it cause the error Object reference not set to an instance of an object – ziakhan Jan 01 '14 at 08:06
0

Your code works as is with the way you bind.

I have made it work by defining missing variable (a):

    AddOrder("something", 2, 100);  
    ...
    //assign some default value since the PO did not define the variable at all...
    int a=0;

Now, you are adviced specify datatypes for columns like this:

        dt.Columns.Add("Qty",typeof(int));
        dt.Columns.Add("Unit Price",typeof(Decimal));
        dt.Columns.Add("Amounts",typeof(Decimal));

Also, if you are planning to edit data then after you protect the Amount, you should use an expression with the column as follows:

dt.Columns.Add("Amount", typeof(Decimal), "Qty *[Unit Price]");

Update:

Assuming the error occurs at the bind, try this:

dataGridViewX1.DataSource = null;
dataGridViewX1.DataSource = dt.DefaultView;
NoChance
  • 5,099
  • 2
  • 26
  • 37
0
private void BindDataGrid()
{
    DataTable table = new DataTable();

    // Insert code to populate a DataTable with data. 

    // Bind grid to DataTable.
    dataGrid1.DataSource = table;
}
Ramashankar
  • 1,438
  • 10
  • 14
0

use

dataGridViewX1.DataSource = dt;

insert break point to view the dt value.

Saeed Khan
  • 497
  • 2
  • 10
  • 25
-1

This error is caused because you are trying to access a property of an object that is null.

Whenever you get this sort of error, you can try to step through and debug your source code. This will help you get to the line that is throwing the error.

You can take a look at which object is null and then fix this error by figuring out why this object is null in your code.

jithu
  • 144
  • 1
  • 11