Dotnetbar-populate only Single externally added column of supergrid from table - c#

I am using DotnetBar,
Have used Supergrid and populated its grid panel by data from particular table,
Now I have dynamically added a Column as shown below,
GridPanel panel = superGridControl.PrimaryGrid;
string query = #"select ........";
DataSet ds_classes = RunQuery(query); // RunQuery is method to
// connect to the database and
// execute the query to return Dataset
if (ds_classes.Tables.Count > 0)
{
panel.DataSource = ds_classes.Tables[0].DefaultView;
}
GridColumn gc = new GridColumn();
gc.HeaderText = "Attribute Type";
panel.Columns.Add(gc);
which is shown in below image
here ID and ATRIBUTES column get populated by different dataset, and I just want Attribute Type column should get populated by different dataset,
Any help will be greatly appreciated.

Use inner join in your query to join the two tables and then display that. You can omit the code that adds the column.

Related

Insert multiple records from datagridview to SQL server database

I have a project in C# which the user can choose only one row in datagridview and fill it's data by filling textboxes, but that takes a long time to do, so how can I develop the below code that user can choose a group of names (which the same data should be recorded) by select several rows in datagridview to insert data into SQL server database?
this the code I use after select a separate row:
{
hour.ADD_ADDITIONAL_HOURS(Convert.ToInt32(txtRecID.Text), dtDateFrom.Value.Date, dgActiveEmps.CurrentRow.Cells[0].Value.ToString(), Convert.ToInt32(cmbSymboles.SelectedValue), Convert.ToInt32(txtHours.Text));
}
note: ADD_ADDITIONAL_HOURS is a stored procedure
The easiest way to do this is to use table valued parameters. To do this, firstly create a user defined table type in SQL Server thus:
CREATE TYPE dbo.YourTypeName AS TABLE
(
[ID] [nvarchar(50)]
)
(I am assuming that your Cells[0].value contains a string. If it is another data type, then change the type of ID above appropriately).
Next change the third parameter of your stored procedure to <parameter_name> AS dbo.YourTypeName READONLY). Within the body of your procedure, change the update(?) statement to be based on a join with this parameter - you use it like any other table. It should look something like:
UPDATE t
SET col1 = #param1 ....
FROM YourTable t INNER JOIN #tablevaluedparameter tvp ON tvp.ID = t.ID
Turning to c#, you need to change the third parameter passed to the stored procedure to be SqlDbType.Structured. Finally you need to populate the parameter with the values from SelectedRows. Here you have a choice: the values can be placed in any of DataTable, IEnumerable<SqlDataRecord> or DbDataReader; all of these can be the Value property of the parameter.
For example, using a DataTable your code would look something like this:
var dT = new DataTable();
dT.Columns.Add(new DataColumn("ID", typeof(string));
for (int i = 0: i < dgActiveEmps.SelectedRows.Count; i++)
{
dT.Rows.Add(dgActiveEmps.SelectedRows[i].Cells[0].Value.ToString());
}
yourSqlCommand.Parameters.Add(new SqlParameter
{
ParameterName = "#tablevaluedparameter",
SqlDbType = SqlDbType.Structured,
Value = dT,
TypeName = "dbo.YourTypeName"
});
Obviously you will need to adapt the namings to fit your case, but I hope it should be clear what you need to do.

DataGridView c#, making a new a custom "fill" method

I have a table on the db, which is shown on 2 different datagridviews, but, one is filled by default, showing all the data on the table, and the second one I want to be filled with a condition.
So I'd like to fill that table initialy with a custom select query. How can I achieve that?
pd: I use datasets, in which I created the custom query, so the question should be how do I fill by default a table with certain tableadapter I made.
if you can share your codes, you will find a most right answer quickly.
var query = db.table.Where(x=>x.column1==condition1).ToList();
dataGridView1.DataSource = query (); dataGridView1.DataBind();
Or, if you have filled datatable/dataset you can write
dataGridView1.DataSource = yourDataTable; dataGridView1.DataBind();
You want to filter the data in your DataSet then set the DataSource of your DataGridView to a DataTable created by that filter. The code would look something like:
// Need to create a DataView from your original DataSet
// and set the view to the default view from your DataSet.
DataView view = new DataView();
view = myDataSet.Tables[0].DefaultView;
// Next, create a filter on that view
view.RowFilter = "State = 'CA'";
// Now create a DataTable from the view
// and set the DataSource of the DataGridView to that DataTable.
DataTable dt = view.ToTable("tablename");
dgrdToDisplay.DataSource = dt;

How to INSERT added rows and UPDATE edited ones in a DataGridView?

I have a Form that shows information about a purchase and within this form I have a DataGridView whose columns are dynamically generated based on a table on the database. This DGV contains information about the items in the selected purchase.
Here's an example of how I generate the columns in runtime in an existing DataGridView:
foreach (ColumnDGV columnDGV in columnsDGV)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.Name = columnDGV.A02_CAMPO;
col.HeaderText = columnDGV.A02_TITULO;
col.DataPropertyName = columnDGV.A02_CAMPO;
this.dataGridView.Columns.Add(col);
}
The object ColumnDGV contains information about the column to be created.
Then I select the columns' names from the columns list and perform the sql query based on those columns:
private void CarregarConteudoDGV()
{
SolicitCompraBLL solicitCompraBLL = new SolicitCompraBLL();
// Gets the columns' names to be used in the SELECT statement.
List<string> columnsNames = this.CamposDGV.Select(c => c.A02_CAMPO).ToList();
// Returns a DataTable containing the items of the selected purchase.
this.dataGridView.DataSource = solicitCompraBLL.ObterItensSolicitacao(columnsNames, this.B11_NUM);
}
When the user clicks in the save button I just iterate through all rows in the DGV and add its values to a nested List where each item is a list of string[2], where the position 0 contains the column name and position 1 its value. Then I pass this List to a method that creates the sql command to update the records.
I can already save the changes of each item in the database, but I also have to insert the added items. How do I know which items should be inserted and which ones should be updated?
When you insert an object in DataBase, generally your object don't have yet an Id. The object to be update must have their Id's existing yet. Now you should see if the object has an Id or Not. If the Id is Zero (or does not exist) then you insert it, otherwise you update it.

DataTable doesn't bind columns caption

My issue is almost the same from this one: Why DataColumn.Caption doesn't work?
But there, he manually created his DataTable, while in my case I'm binding it to an Access database through an OleDbConnection with a connection string. In that database, I have set the captions for each column of the tables so that I could only do the following:
DataTable t = cnn.Consulta(sSQL); //returns a DataTable with a string SQL select query.
DataGridView dg = new DataGridView();
dg.DataSource = t;
int i = 0;
string caption;
foreach (DataGridViewColumn cln in dg.Columns)
{
caption = t.Columns[i].Caption;
cln.HeaderText = caption;
i++;
}
Note that my foreach block is basically the same as the one given as solution in the post mentioned above, but as I have set the captions directly in the Access file, instead of doing it in the code, C# is keeping binding it as the column name, as if they were empty (???).
Now imagine if I had tons of tables, each of them with tons of columns with a non-friendly name? Well, it's true that I'd still have to set a ton of captions anyway, but it would be better than writing 300 lines of code to assign it to each DataColumn.
Unless there is another property where I can store a string to use as header text, I'm stuck with this.

how to get specific column value after filling dataset with tableadapter

for example:
table1 has two columns "col1" and "col2"
this.table1TableAdapter.FillByParam(appDataSet.table1,param);
this returns 1 row
how to set col1 and col2 values to variable?
It would be great if someone post example with code.
Thanks.
This line fills a given DataTable from the query that is specififed as SelectCommand in the TableAdapter. So you say that it just contains a single record?
You can use FirstOrDefault to get that DataRow. Since it's a strongly typed DataTable you have an autogenerated DataTable and DataRow with typed properties and names that are the same as the ctypes and names in database.
So assuming you have two Label controls which Text properties you want to set from the two properties in the row:
appDataSet.table1Row row = appDataSet.table1.FirstOrDefault();
if(row != null)
{
Label1.Text = row.col1;
Label2.Text = row.col2;
}

Categories

Resources