Populating Data to grid On Form Load and from Grid to textboxes

In this article I will be explaining few of different things:

  1. How to flow data from database to grid on form load.
  2. How to flow data from grid cells to textboxes on cell content click event and double click event of datagridview.
  3. To search record from table and flowing it to the grid.

Below is the windows form which gives us a brief idea of what we are trying to achieve:

So first we will create a table.

Code is as follows:

CREATE TABLE [dbo].[blog_tab_test2]

(

    [empid] [varchar](50) primary key,

    [name] [varchar](max) NOT NULL,

    [age] [numeric](18, 0) NULL,

    [location] [varchar](50) NULL

)

Second, we will have to create stored procedure. We will require two stored procedure, one for populating data from table to grid and another for searching specific record through primary key.

  1. Stored procedure for populating data from table to grid:

CREATE PROCEDURE [dbo].[blog_select2_up]

(

@msg varchar(100) output

)

    -- Add the parameters for the stored procedure here

    

AS

BEGIN try

      

 

SET NOCOUNT ON;

select * from blog_tab_test2;

 

return 0

END try

begin catch

select 'Error!!!Problem Occured During Process'

return 1

end catch

  1. Stored procedure for searching specific record through primary key:

     

CREATE PROCEDURE [dbo].[blog_select_search_up]

(

@empid nvarchar(100),

@msg varchar(100) output

)

    -- Add the parameters for the stored procedure here

    

AS

BEGIN try

    

    SET NOCOUNT ON;

select * from blog_tab_test2 where empid=@empid;

 

return 0

END try

begin catch

select 'Error!!!Problem Occured During Process'

return 1

end catch

Finally we would code our application in C #:

In the above form if you double click the circled area which is in red, you would get all the values of that row into textboxes of groupbox1.The above functionality is achieved by CellDoubleClick event of datagridview.

In the above form I have shown how the value of a cell is transferred to textbox. The above functionality is achieved by CellContentClick event of datagridview.

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Data.SqlClient;

 

namespace blog

{

public partial class Form3_dataflowongrid : Form

{

SqlConnection con;

string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=your catalog;Data Source=your data source";

string sp_e_msg;

SqlCommand cmd;

SqlDataAdapter adpt;

DataTable dt;

int proc = 0;

string notific;

public Form3_dataflowongrid()

{

InitializeComponent();

}

 

private void Form3_dataflowongrid_Load(object sender, EventArgs e)

{

try

{

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

cmd = new SqlCommand("blog_select2_up", con);

cmd.CommandType = CommandType.StoredProcedure;

//******************************************************************************

SqlParameter parm2 = new SqlParameter("@returnval", SqlDbType.Int);

parm2.Size = 50;

parm2.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(parm2);

//**************************************************************

SqlParameter parm1 = new SqlParameter("@msg", SqlDbType.VarChar);

parm1.Size = 50;

parm1.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);

//***************************************************************

adpt = new SqlDataAdapter(cmd);

dt = new DataTable();

adpt.Fill(dt);

dataGridView1.DataSource = dt.DefaultView;

cmd.ExecuteNonQuery();

notific = cmd.Parameters["@returnval"].Value.ToString();

//*************************************************************************

sp_e_msg = cmd.Parameters["@msg"].Value.ToString();

 

}

 

catch (Exception ex)

{

 

if (notific == "1")

{

label1.Text = sp_e_msg + notific;

}

}

finally

{

con.Close();

con.Dispose();

cmd.Dispose();

 

}

}

 

//***********cell double click**************************************

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

{

textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();

textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();

textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();

textBox6.Text = dataGridView1.SelectedCells[3].Value.ToString();

 

}

 

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

textBox4.Text = dataGridView1.SelectedCells[0].Value.ToString();

}

 

 

//**********************search*************************************

private void button1_Click(object sender, EventArgs e)

{

try

{

 

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

cmd = new SqlCommand("blog_select_search_up", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@empid", SqlDbType.NVarChar).Value = textBox5.Text;

//******************************************************************************

SqlParameter parm2 = new SqlParameter("@returnval", SqlDbType.Int);

parm2.Size = 50;

parm2.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(parm2);

//**************************************************************

SqlParameter parm1 = new SqlParameter("@msg", SqlDbType.VarChar);

parm1.Size = 50;

parm1.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);

//***************************************************************

adpt = new SqlDataAdapter(cmd);

dt = new DataTable();

adpt.Fill(dt);

dataGridView1.DataSource = dt.DefaultView;

cmd.ExecuteNonQuery();

notific = cmd.Parameters["@returnval"].Value.ToString();

//*************************************************************************

sp_e_msg = cmd.Parameters["@msg"].Value.ToString();

 

}

 

catch (Exception ex)

{

 

if (notific == "1")

{

label1.Text = sp_e_msg + notific;

}

}

finally

{

con.Close();

con.Dispose();

cmd.Dispose();

 

 

}

}

 

 

}

}

Tags: , ,

Windows Form