adding rows to datatable displayed in datagridview

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have this code:
private void EditthisResport_Click(object sender, EventArgs e)<br/>
{
this.Validate();<br/>
DataTable dt = new DataTable();<br/>
dt.AcceptChanges();
if (sConnectionString.Length > 0)<br/>
{
OleDbConnection cn = new OleDbConnection(sConnectionString);<br/>
{<br/>
Excel.Application excelApp = new Excel.Application();
<br/>
cn.Open();<br/>

<br/>
OleDbDataAdapter Adpt = new OleDbDataAdapter("Select* from [Main$]", sConnectionString);<br/>
closeConnection();<br/>
Adpt.Fill(dt);<br/>

<br/>

<br/>
dataGridViewCon.DataSource = dt;<br/>
dataGridViewCon.Columns[0].Name = ("Column1");<br/>
dataGridViewCon.Columns[1].Name = ("Column2");<br/>
dataGridViewCon.Columns[2].Name = ("Column3");<br/>
dataGridViewCon.Columns[3].Name = ("Column4");<br/>
dataGridViewCon.Columns[4].Name = ("Column5");<br/>
dataGridViewCon.Columns[5].Name = ("Column6");<br/>
dataGridViewCon.Columns[6].Name = ("Column7");
string yardre;<br/>
if (comboBox1.SelectedItem != null)<br/>
{<br/>
yardre = comboBox1.SelectedItem.ToString();<br/>
}<br/>
else<br/>
yardre = comboBox1.Text.ToString();<br/>
string SFIkode = SFI.Text.ToString();<br/>
string SystemName = Systeme.Text.ToString();<br/>
string YardRe = yardre;<br/>
string DrawingNr = drawingNR.Text.ToString();<br/>
string commentar = comments.Text.ToString();<br/>
string T = DateTime.Now.ToString();<br/>

<br/>
<br/>
for (int i = 1; i < (dataGridViewCon.Rows.Count - 1); i++)<br/>
{<br/>
if (dataGridViewCon.Rows.Cells["Column7"].Value == null)<br/>
{<br/>

<br/>
string CounterValue = dataGridViewCon.Rows.Cells["Column7"].Value.ToString();<br/>
int CounterTall = Convert.ToInt32(CounterValue);
counter = CounterTall + 1;<br/>
}<br/>
}<br/>

<br/>
dataGridViewCon.AllowUserToAddRows = true;<br/>

<br/>

<br/>

<br/>
dt.Rows.Add(SFIkode, SystemName, YardRe, DrawingNr, T, commentar, counter);<br/>

<br/>

<br/>
excelApp.Quit();<br/>
dt.AcceptChanges();<br/>
GC.Collect();<br/>
GC.WaitForPendingFinalizers();<br/>
Adpt.Dispose();<br/>
closeConnection();<br/>
// SaveCopy();<br/>
}
<br/>
}<br/>
}


But for some reason it only adds one row, evey time i click the button it adds the same row at the same index. So I dont get mutliple rows, I kinda only changes the first row I added, and not adding a new row. Any ideas?

View the full article
 
Back
Top