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?
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 < fp; 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);
}
}
|
Jonathas SucupiraPosted Aug 16, 2011, 2:20 PM
http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx
Robert PercyPosted Aug 16, 2011, 2:16 PM
Jonathas SucupiraPosted Aug 16, 2011, 1:57 PM
Robert PercyPosted Aug 16, 2011, 1:45 PM
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 SucupiraPosted Aug 16, 2011, 12:31 PM
Robert PercyPosted Aug 16, 2011, 9:55 AM
Hennie SmitPosted May 21, 2010, 10:00 AM
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;
}