Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am new to c# , i have one text box and one gridview when user type something in textbox to search, the gridview is filled succesfully when clicked on search button .
but my concern is instaed of search button use ENTER KEY TO search data in gridview.


pls help me to compelete this

thx in advance !!!!
Posted
Updated 29-Nov-12 22:38pm
v2
Comments
Master Vinu 30-Nov-12 6:06am    
THANKS EVERYONE.............

Hi,

Your WebForm code is

XML
<div>

           <asp:TextBox ID="TextBox1" onkeypress="return EnterEvent(event)" runat="server"></asp:TextBox>
           <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
           <br />
           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

       </div>


and you should write a js function

XML
<script>
        function EnterEvent(e) {
            if (e.keyCode == 13) {
                __doPostBack('<%= Button1.ClientID %>', "");
            }
        }
    </script>


And Button Click code is simple,

C#
protected void Button1_Click(object sender, EventArgs e)
      {
          Label1.Text = "Search completed";
      }



You can catch Enter KeyPress like that.

Regards
 
Share this answer
 
v2
Comments
Master Vinu 30-Nov-12 4:37am    
i need for windoes form not for web form sorry i have not mention in question
This is the code how I search for a number value in a datagridview:
C#
private void SearchProject()
{
    int rowIndex = -1;
    bool found = false;

    dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    try
    {
        //Set a string to search for
        int searchValue = int.Parse(tbSearchValue.Text);

        foreach (DataGridViewRow row in dgvProjects.Rows)
        {
            int compareValue = int.Parse(row.Cells[2].Value.ToString());

            if (compareValue.Equals(searchValue))
            {
                found = true;
                rowIndex = row.Index;
                dgvProjects.Rows[row.Index].Selected = true;
                dgvProjects.FirstDisplayedScrollingRowIndex = rowIndex;
                break;
            }
        }

        if (!found)
            MessageBox.Show("Project number not found.\n\nBe sure you're searching for the right project number.", "Project not found",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    catch (FormatException)
    {
        MessageBox.Show("Your input is not a number.", "Input Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

row.Cells[2] is the column where to look at. So I am searching for values, entered by a user in a textbox, in all rows (the foreach block)in the 3rd column (they are zero-based, for that I'm using value [2]).

Call the SearchProject() on a button Click Event:
C#
private void btnSearch_Click(object sender, EventArgs e)
{
    SearchProject();
}

Or when the user presses Enter in the textbox:
C#
private void tbSearchValue_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
        SearchProject();
}
 
Share this answer
 
private void findjobsap_KeyPress(object sender, KeyPressEventArgs e)
           {
               if (e.KeyChar == (char)13)
               {
                   string sql = "select [DocEntry],[JobCardNo],[ITEMNAME],[OD] ,[PlanQty],[FinalInspectionRemarks] ,[OrderDate] ,[Division] from SAPProduction.ProductionInventoryReport WHERE JobCardNo ='" + findjobsap.Text.ToString() + "'";
                   SqlDataAdapter da = new SqlDataAdapter(sql, objConn1);
                   DataSet ds = new DataSet();
                   objConn1.Open();
                   da.Fill(ds, "ProductionInventoryReport");
                   dataGridView1.DataSource = ds;
                   //dataGridView1.DataBind();
                   dataGridView1.DataMember = "ProductionInventoryReport";
                   objConn1.Close();
                   e.Handled = true;
               }


      }
 
Share this answer
 
try this code.
C#
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)
        {
            ds.Clear();
            fillgrid();
            ControlBox = false;
            usdgv.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            txtwildsearch.Focus();
            this.usdgv.Columns["Password"].Visible = false;
            
        }

        private void fillgrid()
        {
            //ds.Clear();
            //usdgv.DataSource = ds;
            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)
            {

            }
        }
    }
}
 
Share this answer
 
v2
Comments
Master Vinu 30-Nov-12 4:35am    
thx zain--i have tried below code but not working

private void textBox3_TextChanged(object sender, KeyPressEventArgs e1, EventArgs e)
{
if (e1.KeyCode == Keys.Enter)
{
string sql = "[DocEntry],[JobCardNo],[ITEMNAME],[OD] ,[PlanQty],[FinalInspectionRemarks] ,[OrderDate] ,[Division] WHERE JobCardNo ='" + textBox3.Text.ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, objConn1);
DataSet ds = new DataSet();
objConn1.Open();
da.Fill(ds, "ProductionInventoryReport");
dataGridView1.DataSource = ds;
//dataGridView1.DataBind();
dataGridView1.DataMember = "ProductionInventoryReport";
objConn1.Close();
e1.Handled = true;
}


}
i have tried this but not working
Sergey Alexandrovich Kryukov 16-Jun-13 15:34pm    
One biggest abuse in this code is empty catch block, a sure way to have all kind of poorly detectable troubles. Usually, the best solution is to avoid using local try-catch block, but try-finally is fine and often necessary.
—SA

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