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 :)
Hi RIDWAN,
Please find the response to your requirement below,
Requirement |
Response |
|
|
Our
SfDataGrid allows you to add new rows by setting the AddNewRowPosition
shown below,
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.
UG Link:
https://help.syncfusion.com/windowsforms/datagrid/datamanipulation#addnewrow
|
|
|
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.
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(); |
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"]; |
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.
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,
{ // 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
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.