views:

46

answers:

2

How can i insert each time a diffrent row in datatable? The data table is used in a dataGridView to write a log. I need each time to write in a diffrent line (row) in the datatable, when I can't know how many rows I need in runtime. Another thing; the datatable is loaded from a XML file, so there might be already rows in the data table.

What can i do? (in short, I always write in the same row) in C#

EDIT: here is the code:

From some reason, the DateGridView isonly having one row(This is a method that activated in a click of a button)

public void gridStart(string i, string b, string c)
        {
            DataTable dt = new DataTable();//empty table with no schema
            DataColumn colContactID = new DataColumn("Date", typeof(string));
            DataColumn colContactName = new DataColumn("Caller", typeof(string));
            DataColumn colResult = new DataColumn("Result", typeof(string));
            dt.Columns.Add(colContactID);
            dt.Columns.Add(colContactName);
            dt.Columns.Add(colResult);
            DataRow row = dt.NewRow();
            row["Date"] = i;
            row["Caller"] = b;
            row["Result"] = c;
            dt.Rows.Add(row);
}
+1  A: 

You should be able to Use DataTable.NewRow(). There are serveral samples on that MSDN page. If you have more questions please provide some sample code to your answer.

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.DataSource = MyDataTable;
}

private string _fileName = "MyCache.xml";

private DataTable _myDataTable;
public DataTable MyDataTable
{
    get
    {
        if (_myDataTable == null)
        {
            _myDataTable = new DataTable();
            if (File.Exists(_fileName))
                _myDataTable.ReadXml(_fileName);
            else
                InitDataTable(_myDataTable);
        }
        return _myDataTable;
    }
}

private void InitDataTable(DataTable table)
{
    table.TableName = "MyTable";
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("Caller", typeof(string));
    table.Columns.Add("Result", typeof(string));
}

// Have your add code call this method!
private void AddValue(DateTime date, string caller, string result)
{
    var row = MyDataTable.NewRow();
    row["Date"] = date;
    row["Caller"] = caller;
    row["Result"] = result;
    MyDataTable.Rows.Add(row);
}

protected override void OnClosed(EventArgs e)
{
    if (_myDataTable != null)
        _myDataTable.WriteXml(_fileName,  XmlWriteMode.WriteSchema);
    base.OnClosed(e);
}
Matthew Whited
I added more information.
Gilad Naaman
@Gilad, I added a little more than you were asking for but I hope it helps. You could also pass in the table you want to add the row to as a parameter to the `AddValue(...)` method.
Matthew Whited
Compiler does not recognize "File.Exists"On what namespace should i use?
Gilad Naaman
add "using System.IO;" to the top of your source code.
Matthew Whited
thank you vey much! :D
Gilad Naaman
+1  A: 

If it is bound to a data source, you need to first get the data source,

// Assuming it's a DataTable
DataTable dt = ((DataTable)myDataGridView.DataSource);  

insert rows to your data source (like what your method is doing in your post), then tell the view to refresh its contents.

So maybe something like this would work:

public void gridStart()
{
    DataTable dt = new DataTable();
    DataColumn colContactID = new DataColumn("Date", typeof(string));
    DataColumn colContactName = new DataColumn("Caller", typeof(string));
    DataColumn colResult = new DataColumn("Result", typeof(string));
    dt.Columns.Add(colContactID);
    dt.Columns.Add(colContactName);
    dt.Columns.Add(colResult);
    dataGridView1.DataSource = dt;
    // Call method to insert values.
}

to start up the grid, and:

public void gridInsert(string i, string b, string c)
{
    DataTable dt = (DataTable)myDataGridView.DataSource; 
    DataRow row = dt.NewRow();
    row["Date"] = i;
    row["Caller"] = b;
    row["Result"] = c;
    dt.Rows.Add(row);
    // Call another method to refresh grid view.
}

to call when you want to insert data to your DataTable.

Duracell
That's what i did, it isn't working. Maybe you can tell me if my code is wrong?
Gilad Naaman
I just edited my answer. It would be easier to know if your DataGridView is binded to a data source like a DataTable.
Duracell
Yes, it is: dataGridView1.DataSource = dt;
Gilad Naaman