Hi
I am creating a WinForm app and one form I have a dgv which is databound to a SQL database. I have almost got it figured out how to programatically add more roes to the dgv, but have some obstacles to overcome. The code below is from the event where everything happens.
It reads the RefRate_Acc table, filtered by Year, Month, Account. If any records are found, they are displayed in the dgv.
If none are found, then the ForexPairSelected table is read. Unique values are stored in a new datatable, and the dgv is populated with the values. The dgv may be populated with only one new record or as many as 25 new records, before the user makes any changes to the data.
This works 100% if only one record needs to ne inserted into the dgv.
However, I get an Index out of range error message on subsequent loops of the for statement.
What am I doing wrong?
PowerShell
private void
btnSearch_Click(object sender, EventArgs e)
{
try
{
this.RefYear = int.Parse(this.cmbYear.Text.ToString());
this.RefMonth = int.Parse(this.cmbMonth.Text.ToString());
this.AccID = int.Parse(this.txtAccID.Text);
// TODO: This
line of code loads data into the 'dsPortMan.RefRate_Acc'
table.
// You can move,
or remove it, as needed.
this.taRefRate_Acc.FillByAcc(this.dsPortMan.RefRate_Acc, this.RefYear, this.RefMonth, this.AccID);
this.rc = this.dsPortMan.RefRate_Acc.Count;
if (this.rc > 0)
{
this.taRefRate_Acc.FillByAcc(this.dsPortMan.RefRate_Acc, this.RefYear, this.RefMonth, this.AccID);
}
else
{
this.taForexPairSel.FillByAcc(this.dsPortMan.ForexPairSelection, this.AccID);
DataTable dtUniqueForex = this.dsPortMan.Tables["ForexPairSelection"].DefaultView.ToTable(true, "ForexPairID",
"ForexPairCode");
this.cmbForexPairSelID.DataSource =
dtUniqueForex;
this.cmbForexPairSelID.DisplayMember =
"ForexPairID";
this.cmbForexPairSel.DataSource =
dtUniqueForex;
this.cmbForexPairSel.DisplayMember =
"ForexPairCode";
this.fp = dtUniqueForex.Rows.Count;
for (int i = 0; i this.dgvAccRefRate[1, i].Value = this.cmbYear.Text;
this.dgvAccRefRate[2, i].Value = this.cmbMonth.Text;
this.dgvAccRefRate[3, i].Value = this.txtAccID.Text;
this.dgvAccRefRate[4, i].Value = this.cmbAccount.Text;
this.dgvAccRefRate[5, i].Value = this.cmbForexPairSelID.Text;
this.dgvAccRefRate[6, i].Value = this.cmbForexPairSel.Text;
if (fp > 1)
{
this.dsPortMan.RefRate_Acc.NewRow();
this.dgvAccRefRate.Refresh();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "An Error has
occured", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
7 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Robert Percy6 reputationPosted Aug 16, 2011, 5:55 AM EDT
0
I have the same problem. My Error states that I cannot ADD ROWS to a Databound Object(DataGridView). I have tried just about all the methods that are out there, NO SUCCESS. If you find one, Please let me know. Thanx...
Robert Percy6 reputationPosted Aug 16, 2011, 9:45 AM EDT
0
I have tried everything in my VB 2008 Databases book, Ive even used the actual code from 5 of the projects in the book and still get the same errors. I am trying to add ID3v1 tag info to a database for the 1100+ cd's I have ripped so far. I have another 1200 to go yet. I have been pulling my imaginary hair out for about a week with no results. I have tried alot of code so far and nothing seems to work. CANNOT ADD ROWS Programmatically to a databound DataGridView. Here is the code I am using...
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
instead of adding a row to the grid itself, you should add a new row to the datasource (I am not sure what datasource type you are using). After you add the row to the datasource just bind it again to the grid.
Hennie Smit8 reputationPosted May 21, 2010, 6:00 AM EDT
DataRow drARR;
this.fp = dtUniqueForex.Rows.Count;
for (int i = 0; i < fp; i++)
{
this.cmbForexPairSelID.SelectedIndex = i;
this.cmbForexPairSel.SelectedIndex = i;
drARR = this.dsPortMan.RefRate_Acc.NewRow();
drARR[1] = this.RefYear;
drARR[2] = this.RefMonth;
drARR[3] = this.AccID;
drARR[4] = this.cmbForexPairSelID.Text;
drARR[5] = 0;
drARR[6] = "A_RR";
this.dsPortMan.RefRate_Acc.BeginInit();
this.dsPortMan.RefRate_Acc.Rows.Add(drARR);
this.dsPortMan.RefRate_Acc.EndInit();
this.dgvAccRefRate.Refresh();
this.dgvAccRefRate[4, i].Value = this.cmbAccount.Text;
this.dgvAccRefRate[6, i].Value = this.cmbForexPairSel.Text;
}
Robert Percy6 reputationPosted Aug 16, 2011, 5:55 AM EDT
Jonathas Sucupira122 reputationPosted Aug 16, 2011, 8:31 AM EDT
Robert Percy6 reputationPosted Aug 16, 2011, 9:45 AM EDT
I am trying to add ID3v1 tag info to a database for the 1100+ cd's I have ripped so far. I have another 1200 to go yet. I have been pulling my imaginary hair out
for about a week with no results. I have tried alot of code so far and nothing seems to work. CANNOT ADD ROWS Programmatically to a databound DataGridView.
Here is the code I am using...
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
ofd.InitialDirectory = GetFolderPath(SpecialFolder.MyMusic)
clsDXaudio = New clsDirectXAudio
If
For Each Me.fName In ofd.FileNames
lstSongs.Items.Add(Me.fName)
ofd.ShowDialog = Windows.Forms.DialogResult.OK Then NextDataProgress.Maximum = lstSongs.Items.Count - 1
DataProgress.Value = 0
fileInfo = New ID3v1()'Lets get the time length of the mp3 file.
fileInfo.FileName = lstSongs.Items(i).ToString()
_title = fileInfo.Track
_artist = fileInfo.Artist
_genre = fileInfo.Genre
For i As Integer = 0 To lstSongs.Items.Count - 1
Dim secx, minx, mediaTotal As String
Dim mediaTimes As StringclsDXaudio.LoadMedia(lstSongs.Items(i).ToString())
mediaTotal = clsDXaudio.Duration
clsDXaudio.UnLoadMedia(lstSongs.Items(i).ToString())
secx = CStr(Int(clsDXaudio.MediaDuration))CStr(CDbl(mediaTotal) - (Int(CDbl(mediaTotal) / 60) * 60))
minx = CStr(Int((CDbl(mediaTotal) - Int(CDbl(mediaTotal) / 3600) * 3600) / 60))
mediaTimes = minx.ToString() & ":" & secx.ToString
_length = mediaTimes
_album = fileInfo.Album
lblStatus.Text = "Updating..."
Dim arr As String() = {_title, _artist, _genre, _length, _album} 'TblTagDataDataGridView.CurrentRow.Cells(1).Value = _title
'TblTagDataDataGridView.CurrentRow.Cells(2).Value = _artist
'TblTagDataDataGridView.CurrentRow.Cells(3).Value = _genre
'TblTagDataDataGridView.CurrentRow.Cells(4).Value = _length
'TblTagDataDataGridView.CurrentRow.Cells(5).Value = _albumTblTagDataDataGridView.Update()
DataProgress.Value = i
NextlblStatus.Text =
"Completed Update..." End If
End SubTblTagDataDataGridView.Rows.Add(arr)
Jonathas Sucupira122 reputationPosted Aug 16, 2011, 9:57 AM EDT
Robert Percy6 reputationPosted Aug 16, 2011, 10:16 AM EDT
Jonathas Sucupira122 reputationPosted Aug 16, 2011, 10:20 AM EDT
http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx