I have a winform app, where in i have a grid view that is supposed to search data from database and display to a gridview.
here is my code to create columns and add row values to it..
while (dr.Read())
{
DataGridViewColumn ad = new DataGridViewColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell(); //Specify which type of cell in this column
ad.CellTemplate = cell;
ad.HeaderText = "Serial No";
ad.Name = "Serial No";
ad.Visible = true;
dataGridView2.Columns.Add(ad);
DataGridViewColumn ad1 = new DataGridViewColumn();
DataGridViewCell cell1 = new DataGridViewTextBoxCell(); //Specify which type of cell in this column
ad1.CellTemplate = cell1;
ad1.HeaderText = "Enrollment No.";
ad1.Name = "Enrollment No.";
ad1.Visible = true;
dataGridView2.Columns.Add(ad1);
DataGridViewColumn ad2 = new DataGridViewColumn();
DataGridViewCell cell2 = new DataGridViewTextBoxCell(); //Specify which type of cell in this column
ad2.CellTemplate = cell1;
ad2.HeaderText = "Student Name";
ad2.Name = "Studen Name";
ad2.Visible = true;
dataGridView2.Columns.Add(ad2);
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView2);
row.Cells[0].Value = dr.GetValue(0).ToString();
row.Cells[1].Value = dr.GetValue(1).ToString();
row.Cells[2].Value = dr.GetValue(2).ToString();
//row.Cells[3].Value = dr.GetValue(3).ToString();
//row.Cells[4].Value = dr.GetValue(4).ToString();
//row.Cells[5].Value = dr.GetValue(5).ToString();
dataGridView1.Rows.Add(row);
i++;
}
i have created 3 columns and i'm trying to store three values from database to the three corresponding cells.
but while executing i get the following error
No row can be added to a DataGridView control that does not have columns. Columns must be added first.
what am i doing wrong?
Please let me know. thanks in advance for all your help...
Problem : You are Adding the columns to dataGridView2 but finally you are trying to add the rows to dataGridView1.
Solution : if your intention isto add the rows to the dataGridView2 replace the follwing statement
Replace This:
dataGridView1.Rows.Add(row);
With This:
dataGridView2.Rows.Add(row);
Related
Hi all I have binded my datagridview with DataGridViewComboBoxColumn as follows
dataGridView1.DataSource = db.DT(sql);
DataGridViewComboBoxColumn Column1 = new DataGridViewComboBoxColumn();
Column1.HeaderText = "Status";
Column1.Name = "Column1";
DTchequeStatus = db.DT("SELECT [ID] ,[Status] FROM [ChequeStatus]");
Column1.DisplayMember = "Status";
Column1.ValueMember = "ID";
Column1.DataSource = DTchequeStatus;
foreach (DataRow DR in DTchequeStatus.Rows)
{
// Column1.Items.Add(DR["Status"].ToString());
}
this.dataGridView1.Columns.Add(Column1);
dataGridView1.Columns["Column1"].ReadOnly = false;
foreach (DataGridViewRow DR in dataGridView1.Rows)
{
(DR.Cells["Column1"] as DataGridViewComboBoxCell).Value = (int)DR.Cells["Cheque Status"].Value;
}
But this is resulting in an empty value for my datagridview as follows. What should I do inorder to get the one value selected i tried this Why on Initial load DataGridViewComboBoxColumn showing empty value but doesn't works
Just setting
Column1.DataPropertyName = "ID"
will resolved the problem
Here is a code where I add value to datagridview from datatable. The "First" and "Third" column of the datagridview have been filled with data from the datatable. The problem is for the "Second" and the "Forth" column, as I have to make it a combobox for the user to choose. Each combobox has default value which is "columnDefaultValue".
string sqlMatchedData = "SELECT colA, colB, colC, colD " +
"FROM TB_LOOKUP_COLUMN "
ds = databaseManager.GetData(sqlMatchedData);
dataGridView1.ColumnCount = 4;
dataGridView1.Columns[0].Name = "First";
dataGridView1.Columns[1].Name = "Second";
dataGridView1.Columns[2].Name = "Third";
dataGridView1.Columns[3].Name = "Forth";
foreach(DataRow row in ds.Tables[0].Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = row[0].ToString();
dataGridView1.Rows[n].Cells[2].Value = row[3].ToString();
string columnDestination = row[1].ToString();
string columnType = row[2].ToString();
comboboxDestinationColumn(columnDefaultValue);
}
How can I create combobox using the datatable and bind it to the specific cell?
private void comboboxDestinationColumn(string columnDefaultValue)
{
string sqlLookupColumn = "SELECT colALookUp, colBLookUp FROM TB_LOOKUP_COLUMN";
DataSet dsColumn = databaseManager.GetData(sqlLookupColumn);
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Destination";
combo.Name = "combo";
foreach(DataRow row in dsColumn.Tables[0].Rows)
{
//populate combobox with data from datatable with defaul value columnDefaultValue
}
}
EDITED
I have found the way to add the combobox to datagridview. But i still lacking the way on how can i set the default value for each combobox based on the variable columnDefaultValue
private void comboboxDestinationColumn(string columnDefaultValue, int n)
{
string sqlLookupColumn = "SELECT colALookUp, colBLookUp FROM TB_LOOKUP_COLUMN";
DataSet dsColumn = databaseManager.GetData(sqlLookupColumn);
DataGridViewComboBoxCell comboboxColumn = new DataGridViewComboBoxCell();
foreach (DataRow row in dsColumn.Tables[0].Rows)
{
comboboxColumn.Items.Add(row[1].ToString());
}
dataGridView1.Rows[n].Cells[1] = comboboxColumn;
}
I'm trying to find something like:
comboboxColumn.Selected = true;
What is the proper way to do this?
You couls just set a value of your comboboxcell after creating it. Like after the foreach loop, you should just put comboboxColumn.Value = comboboxColumn.Items[0] //or whichever of all the items you want. That would give your combobox the wanted value.
P.S.: It's a bad practic to name a DataGridViewComboBoxCell like "comboboxColumn"...
I have a datatable that has many columns,
now I have a datagridview that already have columns designed using these codes:
frm.RationFeedsdataGridView.ColumnCount = 4;
frm.RationFeedsdataGridView.Columns[1].HeaderText = "Number";
frm.RationFeedsdataGridView.Columns[1].HeaderText = "Name";
frm.RationFeedsdataGridView.Columns[2].HeaderText = "Quantity";
frm.RationFeedsdataGridView.Columns[3].HeaderText = "Percent";
Now I want to bind specific columns of my datatable to these columns of datagridview
somthing like that;
frm.RationFeedsdataGridView.Columns["Name"].DataPropertyName = "FeedName";
that FeedName is header of a column in datatable
what should I do?
this is how I fixed my problem:
frm.RationFeedsdataGridView.AutoGenerateColumns = false;
frm.RationFeedsdataGridView.Columns[1].Name = "Name";
frm.RationFeedsdataGridView.Columns[1].HeaderText = "Name";
frm.RationFeedsdataGridView.Columns[1].DataPropertyName = "FeedName";
frm.RationFeedsdataGridView.Columns[2].Name = "Quantity";
frm.RationFeedsdataGridView.Columns[2].HeaderText = "Quantity";
frm.RationFeedsdataGridView.Columns[2].DataPropertyName = "Quantity";
frm.RationFeedsdataGridView.Columns[3].Name = "Percent";
frm.RationFeedsdataGridView.Columns[3].HeaderText = "Percent";
frm.RationFeedsdataGridView.Columns[3].DataPropertyName = "Percent";
frm.RationFeedsdataGridView.DataSource = DTable;
I am trying to load a csv file to datagridview
and now i want to add filtering to the datagridview
How to do?
Here's how I read and load csv file
openFileDialog1.InitialDirectory = #"C:\";
openFileDialog1.Title = "Open CSV Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "CSV";
openFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files(*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string csvPath = openFileDialog1.FileName;
string rowValue;
// int rowValue = int.Parse(??);
string[] cellValue;
dataGridView1.Rows.Clear();
//dataGridView1.Columns.Clear();
if (System.IO.File.Exists(csvPath))
{
System.IO.StreamReader fileReader = new StreamReader(csvPath);
rowValue = fileReader.ReadLine();
cellValue = rowValue.Split(',');
for (int i = 0; i <= cellValue.Count() - 1; i++)
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = cellValue[i]; //column name , value
column.HeaderText = cellValue[i];
dataGridView1.Columns.Add(column);
// dataGridView1.Columns[].CellType = typeof(Int64);
//Conver.ToString
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; // Korean? 칼럼 헤더 가운데 정렬
// dataGridView1.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
// dataGridView1.Columns[0].DataPropertyName = "field name";
}
while (fileReader.Peek() != -1)
{
rowValue = fileReader.ReadLine();
cellValue = rowValue.Split(',');
dataGridView1.Rows.Add(cellValue);
}
fileReader.Dispose();
fileReader.Close();`
Instead of adding rows directly to DataGridView add them to a DataTable and then set that table as DataSource of your DataGridView, then use that table.DefaultView.RowFilter to filter the DataGridView.
You can simply change your code using below examples.
Create a DataTable:
var table = new DataTable();
Add Column to DataTable:
table.Columns.Add("column name");
Add Row to DataTable:
To add a row using a range for example a string[]:
table.Rows.Add(range);
Set the table as DataSource of the DataGridview
dataGridView1.DataSource = table;
Filter using DataTable:
To filter using the data table, for example to show only rows where FirstName is John:
((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "FirstName = 'John'";
Learn more:
Creating a DataTable
Adding Columns to a DataTable
Adding Data to a DataTable
DataView.RowFilter and Filter Expression.
I am getting this error "No row can be added to a DataGridView control that does not have columns. Columns must be added first." what am i doing wrong here?
//create datagridview1
DataGridView dataGridView1 = new DataGridView();
// DataGridViewColumn column = new DataGridViewTextBoxColumn();
// Initialize the DataGridView.
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = true;
DataGridViewColumn column1 = new DataGridViewTextBoxColumn();
column1.DataPropertyName = "Column1";
column1.Name = "title";
dataGridView1.Columns.Add(column1);
DataGridViewColumn column2 = new DataGridViewTextBoxColumn();
column2.DataPropertyName = "Column2";
column2.Name = "imageurl";
dataGridView1.Columns.Add(column2);
DataGridViewColumn column3 = new DataGridViewTextBoxColumn();
column3.DataPropertyName = "Column3";
column3.Name = "videourl";
dataGridView1.Columns.Add(column3);
DataGridViewColumn column4 = new DataGridViewTextBoxColumn();
column4.DataPropertyName = "Column4";
column4.Name = "done";
dataGridView1.Columns.Add(column4);
try
dataGridView1.Columns.Add("Column","Test");
or if you want to choose coulmn type
DataGridViewColumn newCol = new DataGridViewColumn(); // add a column to the grid
DataGridViewCell cell = new DataGridViewCell(); //Specify which type of cell in this column
newCol.CellTemplate = cell;
newCol.HeaderText = "test2";
newCol.Name = "test2";
newCol.Visible = true;
newCol.Width = 40;
gridColors.Columns.Add(newCol);
from here
You also can see MSDN example