I have a datagridview, I am binding this grid with SQL, I have added five rows in datagrid as same in sql with autogeneratecolumn = false..
my code is
string qry1 = "select modal,Spec,color,Types,reqQty from godownRequsition where gr='" + txtGr.Text + "'";
dataGridView1.DataSource = DAL.GetdataTable(qry1);
the problem is that when I run the program, the rows of datagrid could not show the data. If I finish all the column in datagrid and autogenerate column is true it show the data..
now I want to bind the datagrid with my own generated columns not autogenerated.
You need to create the column by your self like this :
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.HeaderText = "Lama";
int indexCol = grid.Columns.Add(column);
Related
I have datagridview in a tabpage. the datagridview has 6 columns. The 6th column is a comboboxcolumn. I am trying to bind a datasource to the comboboxcells. Each row would have different datasource based on the row number. The datagridview will have 10 rows always. The problem is the comboboxes are not populating any values. It just gives me blank item. Both datagrid and comboboxcolumn datasources showing data table values when if I keep break points.Could someone tell me what I am missing here?
private void BuildFreshAccessMatrix()
{
dataGridView1.AutoGenerateColumns = false;
DataGridViewComboBoxColumn cboPermissionCol = (DataGridViewComboBoxColumn)dataGridView1.Columns[5];
//cboPermissionCol.DataPropertyName = "UserLevelCategoryName";
dataGridView1.DataSource = dataProvider.GetBlankMatrixData();
int i = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)(row.Cells["UserLevelCategoryNameAdd"]);
cboPermission.DataSource = dataProvider.GetPermissionComboData(i);
cboPermission.DisplayMember = "UserLevelCategoryName";
cboPermission.ValueMember = "UserLevelCategoryName";
i += 1;
}
}
Try to change your code to :
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)
dataGridView1.Rows[i].Cells["UserLevelCategoryNameAdd"];
instead of :
DataGridViewComboBoxCell cboPermission = (DataGridViewComboBoxCell)
(row.Cells["UserLevelCategoryNameAdd"]);
Background
I have a datagridview that is bound to a dataset. That dataset stores data from a sql db that is obtained using a SqlCommandBuilder. When the dataset is updated the db is updated too.
This works fine.
However I would like one of the columns to contain a combobox.
To do this I have added a DataGridViewComboBoxColumn and hidden the original column. I've then then tried to add extra items to the combobox and then bound the dataset.
My Code
dataGridView1.DataSource = JoblistDataSet.Tables["Joblist"]; //bind ds to dgv
dataGridView1.Columns["Status"].Visible = false; //hide original column
DataGridViewComboBoxColumn Status = new DataGridViewComboBoxColumn();
//setup combobox
Status.Items.Insert(0, "Select Status");//add extra items
Status.DataSource = JoblistDataSet.Tables["Joblist"];//add original item from ds
Status.HeaderText = #"Status ";
Status.DisplayMember = "Status";
Status.DropDownWidth = 78;
Status.DataPropertyName = "Status";
Status.DisplayIndex = 7;
//add combobox
dataGridView1.Columns.Add(Status);
dataGridView1.Refresh();
What i'm Trying to Achieve
I would like a combobox on one of the datagridview columns. In that combobox, the first item should contain (and be displayed) the row's actual value, the other items should contain the other available items.
Question
However, this doesn't seem to work. All I'm getting is the actual rows actual value. Am i going the wrong way about it. Is there a better way to do this?
I am adding columns to datagrid and displaying in my application
// Create Datagrid
Mytable = new DataTable("My Table");
// Add Columns
Mytable.Columns.Add(My_Localization.Cultures.Resources.UserId);
Mytable.Columns.Add(My_Localization.Cultures.Resources.Name);
// Fill data
DataRow dr = Mytable.NewRow();
dr[0] = UserId;
dr[1] = Name;
// Add itemsource
DetailsDlg.ItemsSource = Mytable.AsDataView();
Now is it possible to change the columns names when my localization languages changes on the fly? Since columns are added in the code, I am not able to change the column headers. If I put the columns in wpf, then I am not able to add the data before assigning the itemsource. How do I solve this?
Assuming you are binding your datagrid from your code behind and you are not using MVVM.
You can always change your desire column in your code behind, like this:
DetailsDlg.Columns[0].Header = "New column name";
I'm wondering how can I create and fill unbound columns values after setting DataSource property of DataGridView.
Suppose we have two simple table in our database:
tblTest:
ID (int)
Fname (nvarchar(50)
Population (int)
I have datagridview named grid in my form1:
DataTable tblTest = new DataTable();//Select * from tblTest
...
grid.DataSource = tblTest;
????
What will I replace with ??? to add unbound Percent column that show percentage of population over all data in tblTest rows.
Our calculation must happen in code side not in DB side...
thank you :)
Just add column to DataTable and use it in same way for adding to grid DataSource
DataTable tblTest = new DataTable(); //SELECT * FROM YourTable
//Calculate sum of population
Decimal overall = tblTest.AsEnumerable().Select(dr => dr.Field<Decimal>("Population")).DefaultIfEmpty().Sum();
tblTest.Columns.Add("Percentage", typeof(Decimal));
foreach(DataRow drow in tblTest.Rows)
{
Decimal percentage = YourPercentageCalculationResult; //Add you calculation
drow.SetField<Decimal>("Percentage", percentage);
}
//Create column "on runtime"
DataGridViewTextBoxColumn temp = New DataGridViewTextBoxColumn();
temp.DataPropertyName = "Percentage"
temp.ReadOnly = true;
grid.Columns.Add(temp);
grid.DataSource = tblTest;
I have a DataTable as a data source of a GridView. I'm adding a combo box the the GridView .
I'd like to be able to add a column to the DataTable that would automatically update with the value the user selects in the GridView. Can anyone help?
and the answer is...
DataTable myTable = getYourDataByMagic();
DataGridViewComboBoxColumn box = new DataGridViewComboBoxColumn();
BindingSource bs = new BindingSource();
bs.add("choice one");
bs.add("choice two");
box.HeaderText = "My Choice";
box.Name = "select";
box.DataSource = bs;
box.DataPropertyName = "select";
myTable.Columns.Add(new DataColumn("select"));
this.dataGridView1.Columns.Add(box);
this.dataGridView1.DataSource = myTable;
now, your "myTable" will update with the values selected in the combobox
I'd put two grids side by side one containing all info and one that just had the blank column. I would update the datatable with the grid that contained the one column. This would be update based on selected index of the previous grid. First thing to come to mind.