telerik winforms linq to radgridview - c#

I want to learn Telerik. But I am too newbee.
I have a table named "makale" and it has 7 column named, "no", "ad", "yazar", "kunye", "ders", "olusturma", "guncelleme"
I created a Link to SQL class and I managed to get data like this:
LinqtoSQLDataContext oc = new LinqtoSQLDataContext ();
var all = oc.makales;
Now I want to fetch this table to Telerik Radgridview. When I use Gridview1.DataSource = all; code, it fetches all columns. But I want to fetch only "ad", "yazar", "kunye" and "ders" columns. And I want to change gridview columns' text like "AD", "YAZAR", "KÜNYE", "ŞİŞKO". But when I use Gridview1.DataSource = all; code, gridview columns' text are same as table's columns' name. And I want to set columns' width too.
Could you help me, how can I do this?

In your case RadGridView generating columns automatically based on the DataSource (Gridview1.DataSource = all;)
My advise generate columns by yourself. Telerik. Generating columns
Set MyRadGridView.AutoGenerateColumns = false;
Then create column
GridViewTextBoxColumn yazarColumn = new GridViewTextBoxColumn("UniqueNameYazarColumn");
yazarColumn.Name = "UniqueNameYazarColumn";
textBoxColumn.HeaderText = "Your header";
textBoxColumn.FieldName = "yazar"; //Field is name of the bounded property of source
//add column to the grid
MyRadGridView.Columns.Add(yazarColumn);
Telerik have good enough documentation and basic examples for Winforms controls
Telerik UI Winforms.. GridView

To solve this problems you have 2 ways to do it.
First you can do the "Gridview1.DataSource = all" and then edit the columns that it has generated, or you can previously set up all columns you want to see and then use the Datasource.
The first one is the easy way to begin with.
After you link using "Gridview1.DataSource = all"
you said that all columns are "no", "ad", "yazar", "kunye", "ders", "olusturma", "guncelleme" but only "ad", "yazar", "kunye" and "ders" should appear.
You need to work all the columns like this >>>>
if you want to edit for the names use this:
this.Gridview1.Columns["put_the_name_of_the_column_here"].HeaderText = "Put the name you want to appear on the screen top of the grid here";
this.Gridview1.Columns["put_the_name_of_the_column_here"].IsVisible = false;
if you want the columns not to appear:
this.Gridview1.Columns["put_the_name_of_the_column_here"].IsVisible = false;
Example:
this.Gridview1.Columns["no"].IsVisible = false;
this.Gridview1.Columns["ad"].HeaderText = "AD";
this.Gridview1.Columns["ad"].IsVisible = true;
this.Gridview1.Columns["yazar"].HeaderText = "YAZAR";
this.Gridview1.Columns["yazar"].IsVisible = true;
and so on.....
I´ve working with Telerik´s for Winforms for 5 years now.... I hope I could help...
See ya..

Related

Can't Add ComboBox to Radgrid

[View][1]I'm creating a bounded radgrid, i was trying to add bounded combobox to rad grid, but column is showing.combobox are't show.
You can find various samples for using RadGrid in combination with combo/dropdownlist provided in this post.
First: you need to be a little more explicit in your question or problem.
Maybe adding a little code or images will help us understand more.
From what i understand, you have a radgridview and you want a specific column to have a combobox as a type.
You can do something like this:
//define a GridViewMultiComboBoxColumn
GridViewMultiComboBoxColumn column = new GridViewMultiComboBoxColumn();
column.Name = "Your column name";
column.HeaderText = "The header text you want to have";
column.FieldName = "Field name, maybe you have a class read from a db";
column.DataSource = ds; //here you need to define your datasource for your combobox
column.ValueMember = "Value"; //depends on what you receive from datasource
column.DisplayMember = "Value"; //depends on what you receive from datasource
//add the column to your radgridview
gridView.MasterTemplate.Columns.Add(column);

Use DatabaseTable Field Description when DataBinding

Im using C#, Linq and DataGridView.
I bind my grid like so.
var db = new DataClassesDataContext();
var results = db.Results;
.... much later on in the code..
dataGridView1.DataSource = results;
It uses the Field names for the columns which saved me heaps of time but is it possible to use the FIELD description so I have better col names for my clients.
What Im doing right now is using UNDERSCORES in field names
ID
USER_NAME
SUR_NAME
so in the ColumnAdded event I do
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
e.Column.HeaderText = e.Column.HeaderText.Replace("_"," ");
}
But its just a work around..
If you know that these columns will always represent specific fields in your Database then you could always just set them up directly after you set the datasource for the GridView.
DataGridView.Columns[0].HeaderText = "Column 1";
DataGridView.Columns[1].HeaderText = "Column 2";
DataGridView.Columns[2].HeaderText = "Column 3";
DataGridView.Columns[3].HeaderText = "Column 4";
It's always going to depend on how different the naming convention in your database is compared with how you want to represent each column in the DataGridView. It wont always be possible to manipulate them like you have done in your example.
One thing I have noticed that maybe missing. If your planning on allowing the user to edit the data in the DataGridView and committing changes to the DataContext, you will need to use a Binding Source between these two objects in order for it to work.

DataPropertyName DataGridViewLinkColumn from two columns in datasource

I have a DataGridView with the columns added manually, for example:
DataGridViewLinkColumn col = new DataGridViewLinkColumn() { HeaderText = "", DataPropertyName = "PropValue", ReadOnly = true };
this.dataGridResults.Columns.Add(col);
The datasource is a stored procedure which returns a list with "PropValue" and "PropText" among others.
My problem is that this DataGridViewLinkColumn must show the text from "PropText" and have its value bound to "PropValue". However it seems that DataPropertyName is not that flexible, it only binds to one data type returned from the datsource.
If the problem explanation isn't clear please let me know.
Ideas?
Thank you in advance
You can use this:
DataGridViewLinkColumn links = new DataGridViewLinkColumn();
links.HeaderText = "Hello";
links.UseColumnTextForLinkValue = true;
links.Text="http://microsoft.com";
links.ActiveLinkColor = Color.White;
links.LinkBehavior = LinkBehavior.SystemDefault;
links.LinkColor = Color.Blue;
links.TrackVisitedState = true;
links.VisitedLinkColor = Color.YellowGreen;
dataGridView.Columns.Add(links);
I got it from this link:
How to add DataGridViewLinkColumn property to dynamically generated columns in DataGridView?
I ended up using two separate columns, with one invisible that contains the other value i needed. binding both columns from the dataset into th same datagridview column is not possible.

Datagridview with multiple Tableadapters, view different colums for each view

I have a datagridview with multiple table adapters. every click on the dg exposes a new table on the same control.
How can i show different columns on click?
I've tried to use
DataGridViewColumn newCol = new DataGridViewColumn();
DataGridViewCell cell = new DataGridViewTextBoxCell();
newCol.CellTemplate = cell;
newCol.HeaderText = "numOfTexts";
newCol.Name = "numOfTexts";
newCol.Visible = true;
dg1.Columns.Add(newCol);
but it doesn't display the cell content, only the column name.
Thanks
You can use:
dg.AutoGenerateColumns = true;
See MSDN
true if the columns should be created automatically; otherwise, false. The default is true.
This is exactly what you need to display all columns of the table adapter.
You can hide the unnecessary columns.
Hope I helped...

c# Binding on specific row in table

Using c# .net 2.0 , I want to bind a textbox to a specific row of my table. In Example :
Table Person
ID NAME PRENOM SPECIAL_CATEGORY
1 BOB BOB mex
2 AL AL tot
3 PO PO pap
I want to bind my textbox on the field name where the row contains special_categeory = 'tot'.
Is it possible? or I need to create a Datarow for this row and binding it.
Assuming you're talking about Winforms and you have your data source as a component on your form already, this is fairly simple.
Drag a new BindingSource onto your form and set its data source to be whatever your existing data source is. You can then specify a filtering expression in the new BindingSource's Filter property in the designer. Bind your TextBox to your new BindingSource and you're all set.
Doing this manually (without the designer) is only marginally more complicated.
BindingSource newSource = new BindingSource();
newSource.DataSource = yourExistingDataSource;
newSource.Filter = "special_categeory = 'tot'";
textBox.DataBindings.Add("Text", newSource, "DataMember");
You should be able to bind via...
myNameTextBox.DataBindings.Add( "Text", MyTable, "NAME" );
myPrenomTextBox.DataBindings.Add( "Text", MyTable, "PRENOM" );
mySpecial_CategoryTextBox.DataBindings.Add( "Text", MyTable, "SPECIAL_CATEGORY" );
I actually have a framework that scrolls through all controls, and if they match a column name in a given table, they immediately bind themselves like above.
Then, when you scroll the grid, it should also refresh the individual text controls in your form too.
If there is some binding that needs to be done, you can follow this pattern:
DataView dv = new DataView(MyTable);
dv.RowFilter = "SPECIAL_CATEGORY = 'tot'";
GridView1.DataSource = dv;
GridView1.DataBind();
But I don't think you bind to a TextBox? You can set the Text property like:
foreach(DataRow dr in MyTable.Rows)
{
if (dr["SPECIAL_CATEGORY"] != DBNull.Value &&
dr["SPECIAL_CATEGORY"].ToString() == "tot")
{
myTextBox.Text = dr["NAME"].ToString()
break;
}
}
I'm going to assume it's Winforms and this is how you can do it:
myTable.DefaultView.RowFilter = "SPECIAL_CATEGORY = 'tot'";
this.textBox1.DataBindings.Add("Text",myTable.DefaultView,"Name");

Categories

Resources