Hi @Shaifali jain , Welcome to Microsoft Q&A,
Regarding your first question, I suggest you add the three columns manually. The middle column is used to store the data you need alone.
// Assume your data source is a List<YourClass>
List<YourClass> yourDataSource = GetYourDataSource(); // Method to get the data source
//Assign the data source to the DataGridView
dataGridView1.DataSource = yourDataSource;
// Explicitly specify the association of each column with the data source
dataGridView1.Columns["Column1"].DataPropertyName = "PropertyName1"; // Associate "Column1" with the "PropertyName1" column of the data source
dataGridView1.Columns["Column2"].DataPropertyName = "PropertyName2"; // Associate "Column2" with the "PropertyName2" column of the data source
dataGridView1.Columns["Column3"].DataPropertyName = "PropertyName3"; // Associate "Column3" with the "PropertyName3" column of the data source
Depending on how you want to use the data, you can also just use three columns of data. But it is more difficult to add data with two columns in one column alone. (You may need to use custom controls, and subsequent operations will be difficult)
Regarding making the created row unselectable: When creating a new row, add a tag to it. And paired with DataGridViewSelectionMode.FullRowSelect so that only the entire row can be selected. Then use the dataGridView1_RowStateChanged event:
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if (e.Row.Tag == "xxx")
{
e.Row.Selected = false;
}
}
You can also use the ReadOnly attribute alone to make the line unmodifiable.
Regarding the second question: You may need to customize the keydown event: determine the focus and then move the focus to where you want it.
Use 'e.Handled = true' to cancel the original operation.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.