Sounds like your code is trying to delete the blank new row from the database--but it isn't in the database yet; it is "detached", and just in the memory of the DataGridView... so that's where you need to remove it. You could do this with a line in your validation event(s), which of course will (or should) be called before you save.
One way to handle this is to prompt the user to completely fill out the new row:
Private Sub dataGridView1_RowValidating(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs)
If dataGridView1.Rows(e.RowIndex) IsNot Nothing AndAlso Not dataGridView1.Rows(e.RowIndex).IsNewRow Then
Dim [error] As String = ""
If dataGridView1.Rows(e.RowIndex).Cells("Field1").Value Is Nothing OrElse dataGridView1.Rows(e.RowIndex).Cells("Field1").Value.ToString().Trim() = "" Then
[error] += " * Field #1" & vbLf
End If
If dataGridView1.Rows(e.RowIndex).Cells("Field2").Value Is Nothing OrElse dataGridView1.Rows(e.RowIndex).Cells("Field2").Value.ToString().Trim() = "" Then
[error] += " * Field #2" & vbLf
End If
If [error] <> "" Then
MessageBox.Show("The following required fields were left blank:" & vbLf & vbLf & [error], "Row Validation Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
e.Cancel = True
End If
End If
End Sub
Another way to handle it is to remove the (empty) row from the DataGridView:
Private Sub dataGridView1_RowValidating(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs)
If dataGridView1.Rows(e.RowIndex) IsNot Nothing AndAlso Not dataGridView1.Rows(e.RowIndex).IsNewRow Then
dataGridView1.Rows.Remove(e.RowIndex)
End If
End Sub
A third popular way this can be handled is to have your validation cancel the editing of the current row when nothing is done:
Private Sub dataGridView1_RowValidating(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs)
Dim drv As DataRowView = TryCast(dataGridView1.CurrentRow.DataBoundItem, DataRowView)
If drv IsNot Nothing AndAlso drv(0) = DBNull.Value Then
drv.CancelEdit()
e.Cancel = True
End If
End Sub
(Also, on a side note: if your primary key (a/k/a identity) field is set to auto-number, you may find that ADO.NET is better able to handle the details of getting the next id and such during saves. For more info, check out the MSDN article on "Retrieving Identity of Autonumber Values" and this great set of examples for both SQL Server and MS Access.)
hook up OnRowDataBound event then do stuff
ASPX (Grid):
<asp:.... OnRowDataBound="RowDataBound"..../>
Code Behind:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1)
{
return;
}
if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){
e.Row.BackColor=Color.Red;
}
}
FOR WinForms:
hook the **DataBindingComplete** event and do stuff in it:
private void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
if (e.ListChangedType != ListChangedType.ItemDeleted)
{
DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone();
red.BackColor=Color.Red;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (r.Cells["FollowedUp"].Value.ToString()
.ToUpper().Contains("NO"))
{
r.DefaultCellStyle = red;
}
}
}
}
Best Answer
Set property AutoSizeColumnMode of datagridview to AllCells and check it.
DataGridView.AutoSizeRowsMode Property
try this
DataGridViewRow.Height Property