Click here to Skip to main content
15,568,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why my DataGridView produce duplicate data on user cell_click inside the DataGridView in c# ?


I have 5 records in the DataGridView, when I select or cell click the DataGridView , the record will assign to the TextBoxs, and then click for the selection of record, then displays me in the DataGridView duplicate 5 records total 10 records in the datagridview.

C#
///this is my formuserslection code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Company_Accounts
{
    public partial class FrmUserSelection : Form
    {
        DataSet ds = new DataSet();
        public FrmUserSelection()
        {
            InitializeComponent();
           
        }

        private void FrmUserSelection_Load(object sender, EventArgs e)
        {
            
            fillgrid();
            ControlBox = false;
            usdgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            txtwildsearch.Focus();
            //this.usdgv.Columns["Password"].Visible = false;
            
        }

        private void fillgrid()
        {
            ConnectionString.cnn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select username as [User Name],Password,Title, userrole,logincomputer as [Login Computer] ,logindate as [Login Date] ,Logintime as [Login Time] from login", ConnectionString.cnn);
            da.Fill(ds, "tbl_login");
            usdgv.DataSource = ds.Tables["tbl_login"];
            ConnectionString.cnn.Close();
        }

        private void usdgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            FrmUser.dr = usdgv.CurrentRow;
            fillgrid();
            this.Close();
        }

        private void btnwildsearch_Click(object sender, EventArgs e)
        {
            FrmUser.dr = usdgv.CurrentRow;
            this.Close();
        }

        private void usdgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            label1.Text ="" +usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s1.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s2.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s3.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
        }

        private void txtwildsearch_TextChanged(object sender, EventArgs e)
        {
            DataView dt = ds.Tables[0].DefaultView;
            string tbcolumn;
            try
            {
                tbcolumn = usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText.ToString();
                dt.RowFilter = string.Format("" + tbcolumn + " ='{0}'", txtwildsearch.Text);
                if (dt.Count > 0)
                {
                    usdgv.DataSource = dt;

                }
                else
                {
                    usdgv.DataSource = 0;
                    // dataGridView1.DataSource = ds.Tables[0];

                    //  MessageBox.Show("Record not Found");

                    // fillgrid();

                }
                if (usdgv.RowCount == 0)
                {
                    usdgv.DataSource = ds.Tables[0].DefaultView;

                }
            }
            catch (Exception)
            {

            }
        }
    }
}


Plz,plz,Please, please AnyOne Can Help me
Posted
Updated 22-Nov-12 22:14pm
v2

1 solution

Hi Afifeen,

Please update your code as below.

C#
///this is my formuserslection code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace Company_Accounts
{
    public partial class FrmUserSelection : Form
    {
        DataSet ds = null; // Set null here
        public FrmUserSelection()
        {
            InitializeComponent();
           
        }
 
        private void FrmUserSelection_Load(object sender, EventArgs e)
        {
            
            fillgrid();
            ControlBox = false;
            usdgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            txtwildsearch.Focus();
            //this.usdgv.Columns["Password"].Visible = false;
            
        }
 
        private void fillgrid()
        {
            ds=new DataSet(); //Create new obj here
            ConnectionString.cnn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select username as [User Name],Password,Title, userrole,logincomputer as [Login Computer] ,logindate as [Login Date] ,Logintime as [Login Time] from login", ConnectionString.cnn);
            da.Fill(ds, "tbl_login");
            usdgv.DataSource = ds.Tables["tbl_login"];
            ConnectionString.cnn.Close();
        }
 
        private void usdgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            FrmUser.dr = usdgv.CurrentRow;
            fillgrid();
            this.Close();
        }
 
        private void btnwildsearch_Click(object sender, EventArgs e)
        {
            FrmUser.dr = usdgv.CurrentRow;
            this.Close();
        }
 
        private void usdgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            label1.Text ="" +usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s1.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s2.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
            s3.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
        }
 
        private void txtwildsearch_TextChanged(object sender, EventArgs e)
        {
            DataView dt = ds.Tables[0].DefaultView;
            string tbcolumn;
            try
            {
                tbcolumn = usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText.ToString();
                dt.RowFilter = string.Format("" + tbcolumn + " ='{0}'", txtwildsearch.Text);
                if (dt.Count > 0)
                {
                    usdgv.DataSource = dt; 
                }
                else
                {
                    usdgv.DataSource = 0;                    
                }
                if (usdgv.RowCount == 0)
                {
                    usdgv.DataSource = ds.Tables[0].DefaultView; 
                }
            }
            catch (Exception)
            { 
            }
        }
    }
}

Hope this will help you.
 
Share this answer
 
v2
Comments
Zain -Ul- Arifeen 23-Nov-12 4:22am    
Dear Sir,

i use the following code,but it does not pass values to the textbox in the user form fro the datagridview and give me this Error in the folowing statement;
Statement:txtusername.text=dr.Cells[0].Value.ToString();

Error:
"An unhandled exception of type 'System.NullReferenceException' occurred in Company Accounts.exe

Additional information: Object reference not set to an instance of an object."



///this is my formuserslection code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Company_Accounts
{
public partial class FrmUserSelection : Form
{
DataSet ds = null; // Set null here
public FrmUserSelection()
{
InitializeComponent();

}

private void FrmUserSelection_Load(object sender, EventArgs e)
{

fillgrid();
ControlBox = false;
usdgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
txtwildsearch.Focus();
//this.usdgv.Columns["Password"].Visible = false;

}

private void fillgrid()
{
ds=new DataSet(); //Create new obj here
ConnectionString.cnn.Open();
SqlDataAdapter da = new SqlDataAdapter("select username as [User Name],Password,Title, userrole,logincomputer as [Login Computer] ,logindate as [Login Date] ,Logintime as [Login Time] from login", ConnectionString.cnn);
da.Fill(ds, "tbl_login");
usdgv.DataSource = ds.Tables["tbl_login"];
ConnectionString.cnn.Close();
}

private void usdgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
FrmUser.dr = usdgv.CurrentRow;
fillgrid();
this.Close();
}

private void btnwildsearch_Click(object sender, EventArgs e)
{
FrmUser.dr = usdgv.CurrentRow;
this.Close();
}

private void usdgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
label1.Text ="" +usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
s1.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
s2.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
s3.Text = "" + usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText + " ";
}

private void txtwildsearch_TextChanged(object sender, EventArgs e)
{
DataView dt = ds.Tables[0].DefaultView;
string tbcolumn;
try
{
tbcolumn = usdgv.Columns[usdgv.CurrentCell.ColumnIndex].HeaderText.ToString();
dt.RowFilter = string.Format("" + tbcolumn + " ='{0}'", txtwildsearch.Text);
if (dt.Count > 0)
{
usdgv.DataSource = dt;
}
else
{
usdgv.DataSource = 0;
}
if (usdgv.RowCount == 0)
{
usdgv.DataSource = ds.Tables[0].DefaultView;
}
}
catch (Exception)
{
}
}
}
}

////and this is the user form code.

public static DataGridViewRow dr;
FrmUserSelection fus = new FrmUserSelection();
private void btnsearch_Click(object sender, EventArgs e)
{
DataCollection();


}

private void DataCollection()
{
fus.ShowDialog();
txtusername.Text = dr.Cells[0].Value.ToString();
txtpassword.Text = dr.Cells[1].Value.ToString();
txttitle.Text = dr.Cells[2].Value.ToString(
Mohd. Mukhtar 23-Nov-12 4:39am    
On Button Search click you need to find the all value of the page. You can not get row of the grid in this fashion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900