10 Replies - 1058 Views - Last Post: 26 September 2012 - 09:57 PM Rate Topic: -----

#1 hiral2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-July 12

Count The Total no of Rows in a DataGridView

Posted 25 September 2012 - 07:45 PM

I have to Form...
1) DataGrid View
2) Data Enter Form


now I wanna acces the Total no of Row in DataGridview...
on the 2nd form


my code is ...


( this code is on 2nd form )

public static RecordCount;
RecordCount = int.Parse(Client_Master_Grid.dgClientMaster.RowCount.ToString());


now problem is that...this will fetch data from the 1st form
but ans is still 0;

now what to do
plzz help me out..

how to write the rowcount code on 2nd form....

Is This A Good Question/Topic? 0
  • +

Replies To: Count The Total no of Rows in a DataGridView

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 12:13 AM

First, why do you convert an integer into a string so you can parse it back into an integer?

Second, I need to see more code. Where does Client_Master_Grid come from? How does it get it's value?
Was This Post Helpful? 0
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 06:12 AM

And just to be clear, are you using a DataGrid or a GridView? I almost feel safe assuming you're talking about a DataGrid...

EDIT: I have to start paying more attention to which forum I'm in...

This post has been edited by h4nnib4l: 26 September 2012 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 hiral2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-July 12

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 09:05 AM

View Posth4nnib4l, on 26 September 2012 - 06:12 AM, said:

And just to be clear, are you using a DataGrid or a GridView? I almost feel safe assuming you're talking about a DataGrid...



DataGridView....

View PostMomerath, on 26 September 2012 - 12:13 AM, said:

First, why do you convert an integer into a string so you can parse it back into an integer?

Second, I need to see more code. Where does Client_Master_Grid come from? How does it get it's value?



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.Data.SqlClient;
using System.Configuration;

namespace OrderProcessAutomation
{
    public partial class Client_Master : Form
    {
        #region Connection

        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Connection"].ToString());

        #endregion

        #region Initialization Variable
        public static String ClientID="";
        public static String ClientName="";
        public static String Gender="";
        public static String Age="";
        public static String Address="";
        public static String State="";
        public static String City="";
        public static String Pincode="";
        public static String Phone="";
        public static String Mobile="";
        public static String EmailID="";
        public static String Remark="";
        public static String Count="";
        public static int RecordCount;
        public static DataTable FirstLastDt;
        public static String FrmBackTrack = "";
        public static int FirstLastCount = 0;
        public static int i;
        public static int d;
        public static int c;
        public static int cnt;

        #endregion


        public Client_Master()
        {
            InitializeComponent();
            FirstLastDt = new DataTable();
            
            
        }

        public void Clearfield()
        {
            txtClientID.Clear();
            txtClientName.Clear();
            rdrMale.Checked = true;
            mtxtAge.Clear();
            txtAddress.Clear();
            cmbState.SelectedIndex = 0;
            cmbCity.SelectedIndex = 0;
            mtxtPincode.Clear();
            mtxtPhone.Clear();
            mtxtMobile.Clear();
            txtEmailID.Clear();
            txtRemark.Clear();
        }

        public void GetMaxCode()
        {
            String Query = "SELECT MAX(CAST(RIGHT(ClientID,4) AS NUMERIC(10))) + 1 FROM ClientMasterTbl";
            SqlCommand cmd12 = new SqlCommand(Query, conn);
            int MaxCode = 0;
            if (conn.State == ConnectionState.Open)
                conn.Close();

            conn.Open();
            SqlDataReader rdr = cmd12.ExecuteReader();
            if (rdr.HasRows)
            {
                rdr.Read();
                MaxCode = int.Parse(rdr[0].ToString());
            }
            rdr.Close();
            conn.Close();

            ClientID = "CL" + String.Format("{0:0000}", MaxCode);
            txtClientID.Text = ClientID;
        }

        private void Client_Master_Load(object sender, EventArgs e)
        {
            lblph.Visible = false;


            #region Add Data To Combobox From Database
            SqlCommand ad = new SqlCommand("select * from CmbArea", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();

            conn.Open();
            SqlDataReader rdr1 = ad.ExecuteReader();

            if (rdr1.HasRows)
            {
                while (rdr1.Read())
                {
                    cmbState.Items.Add(rdr1[0].ToString());
                    cmbCity.Items.Add(rdr1[1].ToString());

                }
            }
            conn.Close();
            #endregion 
            txtClientName.Focus(); 


           
         /*   #region Count Previous
            DataTable refr = new DataTable();
            SqlCommand cmd1 = new SqlCommand("Select Count(*) from ClientMasterTbl", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();
            conn.Open();


            RecordCount = int.Parse(cmd1.ExecuteScalar().ToString());

            conn.Close();

            #endregion */

            if (FrmBackTrack.Contains(":"))
            {



                if (ClientID == "")
                {

                    GetMaxCode();

                    rdrMale.Checked = true;

                    FirstLastCount = RecordCount - 1;
                    cmbState.SelectedIndex = 0;
                    cmbCity.SelectedIndex = 0;
                }
                else
                {
                    #region Fill data in Form
                    txtClientID.Text = ClientID;
                    txtClientName.Text = ClientName;
                    txtAddress.Text = Address;
                    txtEmailID.Text = EmailID;
                    txtRemark.Text = Remark;
                    mtxtAge.Text = Age;
                    mtxtMobile.Text = Mobile;
                    mtxtPhone.Text = Phone;
                    mtxtPincode.Text = Pincode;
                    cmbCity.Text = City;
                    cmbState.Text = State;
                    if (Gender == "M")
                        rdrMale.Checked = true;
                    else
                        rdrFemale.Checked = true;
                    FirstLastCount = int.Parse(Count);
                    #endregion
                }

            }

            else
            {
                
              
               Client_Master_Grid.dtClientMaster = new DataTable();
               Client_Master_Grid.dgClientMaster = new DataGridView();
                

             
                 //FirstLastCount = RecordCount - 1;
              Clearfield();
              GetMaxCode();

            }



        }



        /*public void refreshGrid()
        {
            #region Count Previous
            SqlCommand cmd1 = new SqlCommand("Select Count(*) from ClientMasterTbl", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();
            conn.Open();


            RecordCount = int.Parse(cmd1.ExecuteScalar().ToString());

            conn.Close();

            #endregion 
        } */

        #region Operaional Button ( Add , Update , Dele....)

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
                #region Validation  
            String EmailVal = txtEmailID.Text;
            if (txtClientName.Text != "" && txtAddress.Text != "" && mtxtMobile.Text.Length == 10 )
            {
            #endregion 

                #region Add dat to Database
                String g = "";
                if (rdrMale.Checked == true)
                    g = "M";
                else
                g = "F";
                
                String add = "insert into ClientMasterTbl(ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark) values('" + txtClientID.Text + "','" + txtClientName.Text + "','" + g + "','" + mtxtAge.Text + "','" + txtAddress.Text + "','" + cmbState.Text + "','" + cmbCity.Text + "','" + mtxtPincode.Text + "','" + mtxtPhone.Text + "','" + mtxtMobile.Text + "','" + txtEmailID.Text + "','" + txtRemark.Text + "')";
                
                SqlCommand cmd = new SqlCommand(add, conn);
                
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Open();
             
                cmd.ExecuteNonQuery();
                conn.Close();
                
                #endregion

                MessageBox.Show("Data Inserted Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clearfield();
                GetMaxCode();

                #region Load data into Grid
               
                Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;
                //Client_Master_Grid cmg = new Client_Master_Grid();
                Client_Master_Grid.dtClientMaster.Rows.Clear();
                String load = "select * from ClientMasterTbl";
                SqlDataAdapter adpt = new SqlDataAdapter(load, conn);
                adpt.Fill(Client_Master_Grid.dtClientMaster);
                
                 
                Client_Master_Grid.dgClientMaster.Refresh();
                
                #endregion
                i = 1;

            }
            else
                MessageBox.Show("please Enter Required Data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            
            
           
          
            
            


        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            
            Clearfield();
            GetMaxCode();
            rdrMale.Checked = true;
              
            txtClientName.Focus();
            
          
            
        }

        public void ShowDataInForm()
        {
            
            txtClientID.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[0].ToString();
            txtClientName.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[1].ToString();
          
            if (FirstLastDt.Rows[FirstLastCount].ItemArray[2].ToString() == "M")
            {
                rdrMale.Checked = true;
            }
            else
            {
                rdrFemale.Checked = true;
            }
            mtxtAge.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[3].ToString();
            txtAddress.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[4].ToString();
            cmbState.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[5].ToString();
            cmbCity.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[6].ToString();
            mtxtPincode.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[7].ToString();
            mtxtPhone.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[8].ToString();
            mtxtMobile.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[9].ToString();
            txtEmailID.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[10].ToString();
            txtRemark.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[11].ToString();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

            if (rdrMale.Checked == true)
                Gender = "M";
            else
                Gender = "F";

           if (txtClientName.Text != "" && txtAddress.Text != "" && mtxtMobile.Text.Length == 10)
            {

                #region Update The Data
                String upd = "update ClientMasterTbl set ClientName='" + txtClientName.Text + "',Gender='" + Gender + "',Age = '" + mtxtAge.Text + "',Address= '" + txtAddress.Text + "',State = '" + cmbState.Text + "',City= '" + cmbCity.Text + "',Pincode = '" + mtxtPincode.Text + "',Phone = '" + mtxtPhone.Text + "',Mobile = '" + mtxtMobile.Text + "',EmailID = '" + txtEmailID.Text + "',Remark = '" + txtRemark.Text + "' where ClientID = '" + txtClientID.Text + "'";

                SqlCommand cmd2 = new SqlCommand(upd, conn);
                if (conn.State == ConnectionState.Open)
                    conn.Close();

                conn.Open();
                cmd2.ExecuteNonQuery();
                conn.Close();
                #endregion

                MessageBox.Show("Data is Updated Successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clearfield();
                this.Close();

                #region Refresh in Grid View ( Update )
                Client_Master_Grid cmg = new Client_Master_Grid();
                cmg.Refresh();
                
                Client_Master_Grid.dtClientMaster.Rows.Clear();
                String load = "select ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark from ClientMasterTbl";
                SqlDataAdapter adpt = new SqlDataAdapter(load, conn);
                adpt.Fill(Client_Master_Grid.dtClientMaster);
                Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;


                GetMaxCode();
                #endregion 
            }
            else
            {
                MessageBox.Show("please Enter Required Data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            #region Delete The Data
            if (MessageBox.Show("Are you sure to delete the Data?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ClientID = txtClientID.Text;

                String del = "DELETE FROM ClientMasterTbl Where ClientID = '" + txtClientID.Text + "'";
                SqlCommand cmd = new SqlCommand(del, conn);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            #endregion

                MessageBox.Show("Data Successfully Deleted", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clearfield();

                #region Refresh The Data In Grid View ( Delete )
                Client_Master_Grid.dtClientMaster.Rows.Clear();
                String del1 = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark From ClientMasterTbl";
                SqlDataAdapter adapt = new SqlDataAdapter(del1, conn);
                adapt.Fill(Client_Master_Grid.dtClientMaster);
                Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;
                #region Get MaxCode
                String Query = "SELECT MAX(CAST(RIGHT(ClientID,4) AS NUMERIC(10))) + 1 FROM ClientMasterTbl";
                SqlCommand cmd1 = new SqlCommand(Query, conn);
                int MaxCode = 0;
                if (conn.State == ConnectionState.Open)
                    conn.Close();

                conn.Open();
                SqlDataReader rdr = cmd1.ExecuteReader();
                if (rdr.HasRows)
                {
                    rdr.Read();
                    MaxCode = int.Parse(rdr[0].ToString());
                }
                rdr.Close();
                conn.Close();

                ClientID = "CL" + String.Format("{0:0000}", MaxCode);
                txtClientID.Text = ClientID;

                #endregion

            }
            this.Close();
            Client_Master_Grid frm = new Client_Master_Grid();
            frm.Refresh();
                #endregion
        }

        #endregion 

        #region Navigation Button

        private void btnFirst_Click(object sender, EventArgs e)
        {
          
            FirstLastCount = 0;
            String First = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM  ClientMasterTbl";
            SqlDataAdapter adapt = new SqlDataAdapter(First, conn);
            adapt.Fill(FirstLastDt);
            ShowDataInForm();
          
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            

            
                

                if (i == 1)
                {

                    Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;
                    cnt = Client_Master_Grid.dtClientMaster.Rows.Count;
                    FirstLastCount = cnt - 1;
                    //Client_Master.FirstLastCount = 0;

                    
                    String Previous = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM ClientMasterTbl";
                    SqlDataAdapter adapt = new SqlDataAdapter(Previous, conn);
                    adapt.Fill(FirstLastDt);
                    ShowDataInForm();

                    i--;
                }

                else
                {
                    #region GetMaxCode
                    String Query = "SELECT MAX(CAST(RIGHT(ClientID,4) AS NUMERIC(10))) + 1 FROM ClientMasterTbl";
                    SqlCommand cmd12 = new SqlCommand(Query, conn);
                    int MaxCode = 0;
                    if (conn.State == ConnectionState.Open)
                        conn.Close();

                    conn.Open();
                    SqlDataReader rdr = cmd12.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        MaxCode = int.Parse(rdr[0].ToString());
                    }
                    rdr.Close();
                    conn.Close();

                    ClientID = "CL" + String.Format("{0:0000}", MaxCode);
                    //txtClientID.Text = ClientID;
                    #endregion

                    if (FirstLastCount > 0)
                    {
                        if (txtClientID.Text == ClientID)
                        {
                            String Previous = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM ClientMasterTbl";
                            SqlDataAdapter adapt = new SqlDataAdapter(Previous, conn);
                            adapt.Fill(FirstLastDt);
                            ShowDataInForm();

                        }
                        else
                        {
                            FirstLastCount--;
                            String Previous = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM ClientMasterTbl";
                            SqlDataAdapter adapt = new SqlDataAdapter(Previous, conn);
                            adapt.Fill(FirstLastDt);
                            ShowDataInForm();

                        }
                    }
                }
            }

       

            
          

        }

        private void btnNext_Click(object sender, EventArgs e)
        {


            #region Count Previous
            SqlCommand cmd1 = new SqlCommand("Select Count(*) from ClientMasterTbl", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();
            conn.Open();


            RecordCount = int.Parse(cmd1.ExecuteScalar().ToString());

            conn.Close();

            #endregion 
            FirstLastCount = RecordCount - 1;

           
            if (FirstLastCount != RecordCount - 1)
            {
              
                FirstLastCount++;
                String Next = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark FROM ClientMasterTbl ";
                SqlDataAdapter adapt = new SqlDataAdapter(Next, conn);
                adapt.Fill(FirstLastDt);
                ShowDataInForm();
                
            }
            else if(FirstLastCount == RecordCount)
            {
               
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {

            
            FirstLastCount = RecordCount - 1;
            String Last = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark From ClientMasterTbl";
            SqlDataAdapter adapt = new SqlDataAdapter(Last, conn);
            adapt.Fill(FirstLastDt);
            ShowDataInForm();
           
        }



this is the Fisrt form Client_master

[quote name='hiral2cool' date='26 September 2012 - 09:04 AM' timestamp='1348675479' post='1709940']

View Posth4nnib4l, on 26 September 2012 - 06:12 AM, said:

And just to be clear, are you using a DataGrid or a GridView? I almost feel safe assuming you're talking about a DataGrid...



DataGridView....

View PostMomerath, on 26 September 2012 - 12:13 AM, said:

First, why do you convert an integer into a string so you can parse it back into an integer?

Second, I need to see more code. Where does Client_Master_Grid come from? How does it get it's value?



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.Data.SqlClient;
using System.Configuration;

namespace OrderProcessAutomation
{
    public partial class Client_Master : Form
    {
        #region Connection

        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Connection"].ToString());

        #endregion

        #region Initialization Variable
        public static String ClientID="";
        public static String ClientName="";
        public static String Gender="";
        public static String Age="";
        public static String Address="";
        public static String State="";
        public static String City="";
        public static String Pincode="";
        public static String Phone="";
        public static String Mobile="";
        public static String EmailID="";
        public static String Remark="";
        public static String Count="";
        public static int RecordCount;
        public static DataTable FirstLastDt;
        public static String FrmBackTrack = "";
        public static int FirstLastCount = 0;
        public static int i;
        public static int d;
        public static int c;
        public static int cnt;

        #endregion


        public Client_Master()
        {
            InitializeComponent();
            FirstLastDt = new DataTable();
            
            
        }

        public void Clearfield()
        {
            txtClientID.Clear();
            txtClientName.Clear();
            rdrMale.Checked = true;
            mtxtAge.Clear();
            txtAddress.Clear();
            cmbState.SelectedIndex = 0;
            cmbCity.SelectedIndex = 0;
            mtxtPincode.Clear();
            mtxtPhone.Clear();
            mtxtMobile.Clear();
            txtEmailID.Clear();
            txtRemark.Clear();
        }

        public void GetMaxCode()
        {
            String Query = "SELECT MAX(CAST(RIGHT(ClientID,4) AS NUMERIC(10))) + 1 FROM ClientMasterTbl";
            SqlCommand cmd12 = new SqlCommand(Query, conn);
            int MaxCode = 0;
            if (conn.State == ConnectionState.Open)
                conn.Close();

            conn.Open();
            SqlDataReader rdr = cmd12.ExecuteReader();
            if (rdr.HasRows)
            {
                rdr.Read();
                MaxCode = int.Parse(rdr[0].ToString());
            }
            rdr.Close();
            conn.Close();

            ClientID = "CL" + String.Format("{0:0000}", MaxCode);
            txtClientID.Text = ClientID;
        }

        private void Client_Master_Load(object sender, EventArgs e)
        {
            lblph.Visible = false;


            #region Add Data To Combobox From Database
            SqlCommand ad = new SqlCommand("select * from CmbArea", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();

            conn.Open();
            SqlDataReader rdr1 = ad.ExecuteReader();

            if (rdr1.HasRows)
            {
                while (rdr1.Read())
                {
                    cmbState.Items.Add(rdr1[0].ToString());
                    cmbCity.Items.Add(rdr1[1].ToString());

                }
            }
            conn.Close();
            #endregion 
            txtClientName.Focus(); 


           
         /*   #region Count Previous
            DataTable refr = new DataTable();
            SqlCommand cmd1 = new SqlCommand("Select Count(*) from ClientMasterTbl", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();
            conn.Open();


            RecordCount = int.Parse(cmd1.ExecuteScalar().ToString());

            conn.Close();

            #endregion */

            if (FrmBackTrack.Contains(":"))
            {



                if (ClientID == "")
                {

                    GetMaxCode();

                    rdrMale.Checked = true;

                    FirstLastCount = RecordCount - 1;
                    cmbState.SelectedIndex = 0;
                    cmbCity.SelectedIndex = 0;
                }
                else
                {
                    #region Fill data in Form
                    txtClientID.Text = ClientID;
                    txtClientName.Text = ClientName;
                    txtAddress.Text = Address;
                    txtEmailID.Text = EmailID;
                    txtRemark.Text = Remark;
                    mtxtAge.Text = Age;
                    mtxtMobile.Text = Mobile;
                    mtxtPhone.Text = Phone;
                    mtxtPincode.Text = Pincode;
                    cmbCity.Text = City;
                    cmbState.Text = State;
                    if (Gender == "M")
                        rdrMale.Checked = true;
                    else
                        rdrFemale.Checked = true;
                    FirstLastCount = int.Parse(Count);
                    #endregion
                }

            }

            else
            {
                
              
               Client_Master_Grid.dtClientMaster = new DataTable();
               Client_Master_Grid.dgClientMaster = new DataGridView();
                

             
                 //FirstLastCount = RecordCount - 1;
              Clearfield();
              GetMaxCode();

            }



        }



        /*public void refreshGrid()
        {
            #region Count Previous
            SqlCommand cmd1 = new SqlCommand("Select Count(*) from ClientMasterTbl", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();
            conn.Open();


            RecordCount = int.Parse(cmd1.ExecuteScalar().ToString());

            conn.Close();

            #endregion 
        } */

        #region Operaional Button ( Add , Update , Dele....)

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
                #region Validation  
            String EmailVal = txtEmailID.Text;
            if (txtClientName.Text != "" && txtAddress.Text != "" && mtxtMobile.Text.Length == 10 )
            {
            #endregion 

                #region Add dat to Database
                String g = "";
                if (rdrMale.Checked == true)
                    g = "M";
                else
                g = "F";
                
                String add = "insert into ClientMasterTbl(ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark) values('" + txtClientID.Text + "','" + txtClientName.Text + "','" + g + "','" + mtxtAge.Text + "','" + txtAddress.Text + "','" + cmbState.Text + "','" + cmbCity.Text + "','" + mtxtPincode.Text + "','" + mtxtPhone.Text + "','" + mtxtMobile.Text + "','" + txtEmailID.Text + "','" + txtRemark.Text + "')";
                
                SqlCommand cmd = new SqlCommand(add, conn);
                
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Open();
             
                cmd.ExecuteNonQuery();
                conn.Close();
                
                #endregion

                MessageBox.Show("Data Inserted Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clearfield();
                GetMaxCode();

                #region Load data into Grid
               
                Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;
                //Client_Master_Grid cmg = new Client_Master_Grid();
                Client_Master_Grid.dtClientMaster.Rows.Clear();
                String load = "select * from ClientMasterTbl";
                SqlDataAdapter adpt = new SqlDataAdapter(load, conn);
                adpt.Fill(Client_Master_Grid.dtClientMaster);
                
                 
                Client_Master_Grid.dgClientMaster.Refresh();
                
                #endregion
                i = 1;

            }
            else
                MessageBox.Show("please Enter Required Data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            
            
           
          
            
            


        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            
            Clearfield();
            GetMaxCode();
            rdrMale.Checked = true;
              
            txtClientName.Focus();
            
          
            
        }

        public void ShowDataInForm()
        {
            
            txtClientID.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[0].ToString();
            txtClientName.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[1].ToString();
          
            if (FirstLastDt.Rows[FirstLastCount].ItemArray[2].ToString() == "M")
            {
                rdrMale.Checked = true;
            }
            else
            {
                rdrFemale.Checked = true;
            }
            mtxtAge.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[3].ToString();
            txtAddress.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[4].ToString();
            cmbState.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[5].ToString();
            cmbCity.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[6].ToString();
            mtxtPincode.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[7].ToString();
            mtxtPhone.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[8].ToString();
            mtxtMobile.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[9].ToString();
            txtEmailID.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[10].ToString();
            txtRemark.Text = FirstLastDt.Rows[FirstLastCount].ItemArray[11].ToString();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

            if (rdrMale.Checked == true)
                Gender = "M";
            else
                Gender = "F";

           if (txtClientName.Text != "" && txtAddress.Text != "" && mtxtMobile.Text.Length == 10)
            {

                #region Update The Data
                String upd = "update ClientMasterTbl set ClientName='" + txtClientName.Text + "',Gender='" + Gender + "',Age = '" + mtxtAge.Text + "',Address= '" + txtAddress.Text + "',State = '" + cmbState.Text + "',City= '" + cmbCity.Text + "',Pincode = '" + mtxtPincode.Text + "',Phone = '" + mtxtPhone.Text + "',Mobile = '" + mtxtMobile.Text + "',EmailID = '" + txtEmailID.Text + "',Remark = '" + txtRemark.Text + "' where ClientID = '" + txtClientID.Text + "'";

                SqlCommand cmd2 = new SqlCommand(upd, conn);
                if (conn.State == ConnectionState.Open)
                    conn.Close();

                conn.Open();
                cmd2.ExecuteNonQuery();
                conn.Close();
                #endregion

                MessageBox.Show("Data is Updated Successfully", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clearfield();
                this.Close();

                #region Refresh in Grid View ( Update )
                Client_Master_Grid cmg = new Client_Master_Grid();
                cmg.Refresh();
                
                Client_Master_Grid.dtClientMaster.Rows.Clear();
                String load = "select ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark from ClientMasterTbl";
                SqlDataAdapter adpt = new SqlDataAdapter(load, conn);
                adpt.Fill(Client_Master_Grid.dtClientMaster);
                Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;


                GetMaxCode();
                #endregion 
            }
            else
            {
                MessageBox.Show("please Enter Required Data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            #region Delete The Data
            if (MessageBox.Show("Are you sure to delete the Data?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ClientID = txtClientID.Text;

                String del = "DELETE FROM ClientMasterTbl Where ClientID = '" + txtClientID.Text + "'";
                SqlCommand cmd = new SqlCommand(del, conn);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            #endregion

                MessageBox.Show("Data Successfully Deleted", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Clearfield();

                #region Refresh The Data In Grid View ( Delete )
                Client_Master_Grid.dtClientMaster.Rows.Clear();
                String del1 = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark From ClientMasterTbl";
                SqlDataAdapter adapt = new SqlDataAdapter(del1, conn);
                adapt.Fill(Client_Master_Grid.dtClientMaster);
                Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;
                #region Get MaxCode
                String Query = "SELECT MAX(CAST(RIGHT(ClientID,4) AS NUMERIC(10))) + 1 FROM ClientMasterTbl";
                SqlCommand cmd1 = new SqlCommand(Query, conn);
                int MaxCode = 0;
                if (conn.State == ConnectionState.Open)
                    conn.Close();

                conn.Open();
                SqlDataReader rdr = cmd1.ExecuteReader();
                if (rdr.HasRows)
                {
                    rdr.Read();
                    MaxCode = int.Parse(rdr[0].ToString());
                }
                rdr.Close();
                conn.Close();

                ClientID = "CL" + String.Format("{0:0000}", MaxCode);
                txtClientID.Text = ClientID;

                #endregion

            }
            this.Close();
            Client_Master_Grid frm = new Client_Master_Grid();
            frm.Refresh();
                #endregion
        }

        #endregion 

        #region Navigation Button

        private void btnFirst_Click(object sender, EventArgs e)
        {
          
            FirstLastCount = 0;
            String First = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM  ClientMasterTbl";
            SqlDataAdapter adapt = new SqlDataAdapter(First, conn);
            adapt.Fill(FirstLastDt);
            ShowDataInForm();
          
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            

            
                

                if (i == 1)
                {

                    Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;
                    cnt = Client_Master_Grid.dtClientMaster.Rows.Count;
                    FirstLastCount = cnt - 1;
                    //Client_Master.FirstLastCount = 0;

                    
                    String Previous = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM ClientMasterTbl";
                    SqlDataAdapter adapt = new SqlDataAdapter(Previous, conn);
                    adapt.Fill(FirstLastDt);
                    ShowDataInForm();

                    i--;
                }

                else
                {
                    #region GetMaxCode
                    String Query = "SELECT MAX(CAST(RIGHT(ClientID,4) AS NUMERIC(10))) + 1 FROM ClientMasterTbl";
                    SqlCommand cmd12 = new SqlCommand(Query, conn);
                    int MaxCode = 0;
                    if (conn.State == ConnectionState.Open)
                        conn.Close();

                    conn.Open();
                    SqlDataReader rdr = cmd12.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        MaxCode = int.Parse(rdr[0].ToString());
                    }
                    rdr.Close();
                    conn.Close();

                    ClientID = "CL" + String.Format("{0:0000}", MaxCode);
                    //txtClientID.Text = ClientID;
                    #endregion

                    if (FirstLastCount > 0)
                    {
                        if (txtClientID.Text == ClientID)
                        {
                            String Previous = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM ClientMasterTbl";
                            SqlDataAdapter adapt = new SqlDataAdapter(Previous, conn);
                            adapt.Fill(FirstLastDt);
                            ShowDataInForm();

                        }
                        else
                        {
                            FirstLastCount--;
                            String Previous = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark  FROM ClientMasterTbl";
                            SqlDataAdapter adapt = new SqlDataAdapter(Previous, conn);
                            adapt.Fill(FirstLastDt);
                            ShowDataInForm();

                        }
                    }
                }
            }

       

            
          

        }

        private void btnNext_Click(object sender, EventArgs e)
        {


            #region Count Previous
            SqlCommand cmd1 = new SqlCommand("Select Count(*) from ClientMasterTbl", conn);
            if (conn.State == ConnectionState.Open)
                conn.Close();
            conn.Open();


            RecordCount = int.Parse(cmd1.ExecuteScalar().ToString());

            conn.Close();

            #endregion 
            FirstLastCount = RecordCount - 1;

           
            if (FirstLastCount != RecordCount - 1)
            {
              
                FirstLastCount++;
                String Next = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark FROM ClientMasterTbl ";
                SqlDataAdapter adapt = new SqlDataAdapter(Next, conn);
                adapt.Fill(FirstLastDt);
                ShowDataInForm();
                
            }
            else if(FirstLastCount == RecordCount)
            {
               
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {

            
            FirstLastCount = RecordCount - 1;
            String Last = "SELECT ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark From ClientMasterTbl";
            SqlDataAdapter adapt = new SqlDataAdapter(Last, conn);
            adapt.Fill(FirstLastDt);
            ShowDataInForm();
           
        }


this is the Fisrt form Client_master
[/quote]




this is the 2nd form 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.Data.SqlClient;
using System.Configuration;

namespace OrderProcessAutomation
{
    public partial class Client_Master_Grid : Form
    {
        #region Connection

        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["Connection"].ToString());

        #endregion

        #region Initialization of DataTable & GridView

        public static DataTable dtClientMaster;
        public static DataGridView dgClientMaster;
        public static int Gridcount;
     

        #endregion

        public Client_Master_Grid()
        {
            InitializeComponent();
        }

        private void panelTop_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Client_Master_Grid_Load(object sender, EventArgs e)
        {
            dgClientMaster = new DataGridView();
            dtClientMaster = new DataTable();
            

            this.panelCenter.Controls.Add(dgClientMaster);
            dgClientMaster.Dock = DockStyle.Fill;
            String load = "select ClientID,ClientName,Gender,Age,Address,State,City,Pincode,Phone,Mobile,EmailID,Remark from ClientMasterTbl";
            //String cmb = "select * from CmbArea";
            SqlDataAdapter adpt = new SqlDataAdapter(load,conn);
            
            adpt.Fill(Client_Master_Grid.dtClientMaster);
            Client_Master_Grid.dgClientMaster.DataSource = Client_Master_Grid.dtClientMaster;
            dgClientMaster.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgClientMaster.EditMode = DataGridViewEditMode.EditProgrammatically;
            dgClientMaster.CellDoubleClick += new DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
            dgClientMaster.RowHeadersVisible = false;
            dgClientMaster.Columns["ClientID"].Visible = false;
            dgClientMaster.AllowUserToAddRows = false;
            
            dgClientMaster.RowTemplate.Height = 28;
            dgClientMaster.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            //dgClientMaster.FirstDisplayedScrollingRowIndex = 10;
            //dgClientMaster.Refresh();
        
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Client_Master objm = new Client_Master();
            Client_Master.FrmBackTrack = "ClientMasterGrid:";

            Client_Master.ClientID = dgClientMaster.Rows[e.RowIndex].Cells["ClientID"].Value.ToString();
            Client_Master.ClientName = dgClientMaster.Rows[e.RowIndex].Cells["ClientName"].Value.ToString();
            Client_Master.Gender = dgClientMaster.Rows[e.RowIndex].Cells["Gender"].Value.ToString();
            Client_Master.Age= dgClientMaster.Rows[e.RowIndex].Cells["Age"].Value.ToString();
            Client_Master.Address= dgClientMaster.Rows[e.RowIndex].Cells["Address"].Value.ToString();
            Client_Master.State= dgClientMaster.Rows[e.RowIndex].Cells["State"].Value.ToString();
            Client_Master.City= dgClientMaster.Rows[e.RowIndex].Cells["City"].Value.ToString();
            Client_Master.Pincode= dgClientMaster.Rows[e.RowIndex].Cells["Pincode"].Value.ToString();
            Client_Master.Phone= dgClientMaster.Rows[e.RowIndex].Cells["Phone"].Value.ToString();
            Client_Master.Mobile= dgClientMaster.Rows[e.RowIndex].Cells["Mobile"].Value.ToString();
            Client_Master.EmailID= dgClientMaster.Rows[e.RowIndex].Cells["EmailID"].Value.ToString();
            Client_Master.Remark = dgClientMaster.Rows[e.RowIndex].Cells["Remark"].Value.ToString();
            Client_Master.Count = dgClientMaster.Rows[e.RowIndex].Index.ToString();

            Gridcount = int.Parse(dgClientMaster.RowCount.ToString());


            objm.ShowDialog();
            

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Client_Master objm = new Client_Master();
            Client_Master.FrmBackTrack = "ClientMasterGrid:";
            Client_Master.ClientID = "";
            objm.ShowDialog();
        }

        private void btnAdd_MouseEnter(object sender, EventArgs e)
        {
            btnAdd.BackColor = Color.LimeGreen;
            btnAdd.ForeColor= Color.White;
        }

        private void btnAdd_MouseLeave(object sender, EventArgs e)
        {
            btnAdd.BackColor = Color.White;
            btnAdd.ForeColor = Color.Black;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 11:33 AM

For the love of god, use code tags. Also, post the relevant parts of your code. Don't dump thousands of lines and expect others to sort through it for you.
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 11:53 AM

First, before you reply, read the text that is already in the reply box and use the tags!
Second, you didn't answer my first question, why do you convert an int to a string and convert it back to an int?
Third, why are your DataGridView and DataTable variables static? You know you overwrite whatever the first form has done when you load the second form? I created a simple project with a DataGridView, added data to it and had no issues getting the row count. I suspect your problem has something to do with your static variables and how you are constantly changing them.
Fourth, it's really bad practice to access the controls this way.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,168
  • Joined: 05-May 12

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 05:40 PM

Have you looked at this tutorial on how to pass data between forms: http://www.dreaminco...-forms-in-c%23/
Was This Post Helpful? 0
  • +
  • -

#8 hiral2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-July 12

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 06:56 PM

View PostSkydiver, on 26 September 2012 - 05:40 PM, said:

Have you looked at this tutorial on how to pass data between forms: http://www.dreaminco...-forms-in-c%23/




but...i have 2forms
but ...in ur project there is no ...
database use..
plus i m not passing data bet two forms
i m passing the data between one form's Datagridview to Other form...

View PostMomerath, on 26 September 2012 - 11:53 AM, said:

First, before you reply, read the text that is already in the reply box and use the tags!
Second, you didn't answer my first question, why do you convert an int to a string and convert it back to an int?
Third, why are your DataGridView and DataTable variables static? You know you overwrite whatever the first form has done when you load the second form? I created a simple project with a DataGridView, added data to it and had no issues getting the row count. I suspect your problem has something to do with your static variables and how you are constantly changing them.
Fourth, it's really bad practice to access the controls this way.




it's my problem..
sorry...there is no need to change string to int
sorry


and datagridview data must refresh when new data added..
one form's datagridview data how to acces in form 2 thats the problem

if i m not using static variable then no probllem..
i also make row count easily..but then refresh the grid in backside is not possible
Was This Post Helpful? 0
  • +
  • -

#9 hiral2cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-July 12

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 07:02 PM

View PostCurtis Rutland, on 26 September 2012 - 11:33 AM, said:

For the love of god, use code tags. Also, post the relevant parts of your code. Don't dump thousands of lines and expect others to sort through it for you.



http://tinyurl.com/clpcv3t
my project..

visual studio 2008
sql server 2005

C# 2.0
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,168
  • Joined: 05-May 12

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 07:10 PM

OMG... You just made things worse. Instead of just giving us the revelant parts of you project, not you are giving us your entire project.

We were hoping that you would cull down the code to the parts where you believe the problem is at. If you weren't going filter down the code, at the very least tell us which lines of code to look at. *sigh*
Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Count The Total no of Rows in a DataGridView

Posted 26 September 2012 - 09:57 PM

View Posthiral2cool, on 26 September 2012 - 06:56 PM, said:

if i m not using static variable then no probllem..
i also make row count easily..but then refresh the grid in backside is not possible

Why do you think it isn't possible? Add a method to the form that refreshes the data grid and call it when you need to refresh the data grid.

Why do you have the two forms in the first place? I think your basic problem here is design, not actually code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1