We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

add a new empty row and search data in sfdatagridview's live row.

what i did to add a new empty row using the default datagridview was to use this code and put it in the click event inside the button :

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            DataGridView1.Rows.Add();
        }

Then how about using sfDatagridview ?

i know adding blank rows directly with sfdatagrid, but what i want is to add them using Button.

Next, after adding blank rows, I want to do a data search based on the data I entered in row 1 in the "Name" column and then display the results in the "City" column.

I'm using this code with the built in datagrid, and can't figure out how to implement it in sfdatagrid :

string SearchName = DataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
string query = "SELECT * FROM tb_person WHERE Name= @Name";

conn.Open();
MySqlCommand Cmd = new MySqlCommand(query, conn);
Cmd.Parameters.AddWithValue("@Name", SearchName);
MySqlDataReader Rdr = Cmd.ExecuteReader();
if (Rdr.Read())
{
    DataGridView1.Rows[e.RowIndex].Cells[1].Value = Rdr["City"];
}
conn.Close();


Thanks :)


4 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team January 20, 2023 03:13 PM UTC

Hi RIDWAN,

Please find the response to your requirement below,

Requirement

Response


I know adding blank rows directly with sfdatagrid, but what i want is to add them using Button.


 

Our SfDataGrid allows you to add new rows by setting the AddNewRowPosition shown below,

sfDataGrid.AddNewRowPosition = RowPosition.Top;

 

When starting editing in AddNewRow, the SfDataGrid control creates an instance for the underlying data object and adds it to the underlying collection when editing is completed. With this, you can add the new rows as you required.

For more information related to AddNewRow, please refer to the below user guide documentation link,

 

UG Link: https://help.syncfusion.com/windowsforms/datagrid/datamanipulation#addnewrow


I want to do a data search based on the data I entered in row 1 in the "Name" column and then display the results in the "City" column.


 

We are a little unclear about your scenario. By default, our Searching feature will work depending on all columns. It searched the matched data and highlighted that.

 

For more information related to Searching, please refer to the below user guide documentation link,

 

UG Link: https://help.syncfusion.com/windowsforms/datagrid/search

But in your scenario, you need to display or filter the particular column with the provided input data. It will be achievable by our filtering feature, here we have attached the filtering demo and the reference video. Please have a look at this.


Regards,
Dhanasekar M.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Sample__Video_Demo_3725c234.zip


RI RIDWAN replied to Dhanasekar Mohanraj January 24, 2023 04:12 AM UTC

thank you in advance for taking the time to reply to my message, and apologize if something is incomplete from my previous question,

What I mean by searching data is:

instead I use textbox to search data inside sfdatagrid, I want it to be done inside sfdatagrid, for example:

I have 3 columns: Id_Student, Name, Address, then I added a blank row, if an empty row in the Id_Student column I put the value "001" then the same row in the Name and Address columns will show the result: "Andreas" ," st.lucas 36-144".

if i use default datagridview i can achieve it by adding  event CellEndEdit :

private void ViewProdukOrderTambahData_CellEndEdit(object? sender, DataGridViewCellEventArgs e)
{
   string SearchName = DataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
   string query = "SELECT * FROM tb_student WHERE Id_Student= @Id_Student";

   conn.Open();
   MySqlCommand Cmd = new MySqlCommand(query, conn);
   Cmd.Parameters.AddWithValue("@Id_Student", SearchName);
   MySqlDataReader Rdr = Cmd.ExecuteReader();
if (Rdr.Read()) { DataGridView1.Rows[e.RowIndex].Cells[1].Value = Rdr["Name"]; DataGridView1.Rows[e.RowIndex].Cells[2].Value = Rdr["Address"]; } conn.Close(); }


Here I use data search from mysql database, and it looks like sfdatagrid doesn't have a CellEndEdit event.

or tell me how to achieve it using SfDatagrid for this Code :

DataGridView1.Rows[e.RowIndex].Cells[1].Value = Rdr["Name"];


DM Dhanasekar Mohanraj Syncfusion Team January 24, 2023 03:56 PM UTC

Hi RIDWAN,


We are analyzing your requirement to "Set the other columns' cell values depending on the entered cell value on the first column" and update you with further details on January 27, 2023.



DM Dhanasekar Mohanraj Syncfusion Team January 27, 2023 01:02 PM UTC

Hi RIDWAN,

As we mentioned earlier, you can add the blank rows by using the SfDataGrid.AddNewRowPosition. And set the cell values for the rest column depending on the committed data for the first column will be achievable by using the SfDataGrid.CurrentCellEndEdit event is shown below,


this
.sfDataGrid1.CurrentCellEndEdit += SfDataGrid1_CurrentCellEndEdit;

private void SfDataGrid1_CurrentCellEndEdit(object sender, CurrentCellEndEditEventArgs e)

{

    // Get the record.

    var data = e.DataRow.RowData as System.Data.DataRowView;

 

    // Get the current cell value

    if (sfDataGrid1.View != null && sfDataGrid1.CurrentCell != null && sfDataGrid1.CurrentCell.CellRenderer != null)

    {

        var currentCellValue = sfDataGrid1.CurrentCell.CellRenderer.GetControlValue();

       

        // Here you can do the required customization based on your scenario

        // Setting the required column values programmatically

        if (currentCellValue.Equals("001"))

        {

            this.sfDataGrid1.View.GetPropertyAccessProvider().SetValue(data, sfDataGrid1.Columns[1].MappingName, "Andreas");

            this.sfDataGrid1.View.GetPropertyAccessProvider().SetValue(data, sfDataGrid1.Columns[2].MappingName, "st.lucas 36-144");

        }

    }

}


Here we have prepared the sample and video demo for your reference, please have a look at this.

For more information related to getting the CurrentCellValue and CurrentCellEndEdit event, please refer to the below user guide documentation link,

UG Link:

https://help.syncfusion.com/windowsforms/datagrid/selection#get-the--cell-value
https://help.syncfusion.com/windowsforms/datagrid/selection#get-the--cell-value

https://help.syncfusion.com/windowsforms/datagrid/editing#display-message-box-on-current-cell-editing-complete

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Sample__Video_c7906eaf.zip

Marked as answer
Loader.
Up arrow icon