Ver Mensaje Individual
  #5 (permalink)  
Antiguo 04/06/2012, 08:00
Avatar de gerson_533
gerson_533
 
Fecha de Ingreso: junio-2012
Mensajes: 3
Antigüedad: 12 años, 9 meses
Puntos: 0
Respuesta: Insertar fila en DataGridView [Ayuda]

yo tengo el mismo problema estoy realizando una factura en c# enla zada a datos con sql, me agrega los datos pero me deja una linea en blanco , como si se reseteara la datagridview, digito el codigo del producto desde la column1 posicion (0,0);cuando se pulsa la tecla inter se ejecuta la consulta select y muestra el registro con los datos en la fila 0, pero cuano en la posicion (0,1), me muestra el resultado en la fila 2 y me deja la fila 1 en blanco. aqui esta el codigo que estoy utilizando , espero me puedan ayudar y gracias.

namespace WindowsFormsApplication1
{
public partial class Form1 : Form

{
SqlConnection miconexion = new SqlConnection("Data Source=PORTATILCD; Initial Catalog=prueba; Integrated Security=True");

int fila = 0;
float total = 0;
string subtotal;
SqlDataAdapter da;
DataTable dt = new DataTable();
DataSet ds = new DataSet();

public Form1()
{
InitializeComponent();
//dataGridView1.Rows.Add(1);

}


private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{

if (e.KeyCode == Keys.Enter)
{

miconexion.Open();

String testValue1 = (String)dataGridView1["Column1", fila].Value.ToString();

SqlCommand consulta = new SqlCommand("select cod_pro as Codigo,nom_prod as Producto,descripcion as Descripcion,costo as Costo,unitario as Unitario from producto where cod_pro='" + testValue1 +"'", miconexion);
da = new SqlDataAdapter(consulta);
da.Fill(dt);

try
{

dataGridView1.AutoGenerateColumns = false;

this.dataGridView1.DataSource = dt;

subtotal = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();


total = total + float.Parse(subtotal);
fila = fila + 1;
miconexion.Close();




}


catch (Exception exp)
{
MessageBox.Show(exp.Message, "Error General");

}


if (dataGridView1.CurrentRow.Index == dataGridView1.RowCount - 1)
{
DataTable dts = null;
dts = dataGridView1.DataSource as DataTable;
DataRow row = dts.NewRow();

dts.Rows.Add(row);
dataGridView1.DataSource = dts;

}

}
textBox3.Text = total.ToString();
}