I'm trying to bind repeater control from database. But i need to bind repeater with unique column.
eg.
Mumbai, Haryana, Goa, Haryana, Mumbai
to
Mumbai, Haryana, Goa
My code is given below.
SqlConnection con3 = new SqlConnection("myconnection");
string strcon = "select * from indiapincodes where statename =#statename";
cmd = new SqlCommand(strcon, con3);
da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("#statename", erouting);
ds = new DataSet();
da.Fill(ds, "emp");
Repeater1.DataSource = ds;
Repeater1.DataBind();
con3.Close();
con3.Dispose();
Above code is working good but i want to know how to filter column with unique value and fill in repeater. Thanks in advance.
string strcon = "SELECT DISTINCT columnname from indiapincodes WHERE statename=#statename";
Related
Winform has 2 grids. I display related tables from the SQL Server database to them.
Here is the code:
string connectionString = ConfigurationManager.ConnectionStrings["connectionSIPiT"].ConnectionString;
string command = "SELECT UserID, UserName,Login, idArm FROM Users";
string command2 = "SELECT id, name FROM arm";
sqlConnection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(command2, sqlConnection);
SqlDataAdapter adapter1 = new SqlDataAdapter(command, sqlConnection);
DataSet dataset1 = new DataSet();
adapter.Fill(dataset1, "arm");
adapter1.Fill(dataset1, "Users");
DataColumn keyColumn = dataset1.Tables[0].Columns[0];
DataColumn foreignKeyColumn = dataset1.Tables[1].Columns[3];
dataset1.Relations.Add("armUsers", keyColumn, foreignKeyColumn);
bindingSource1.DataSource = dataset1;
bindingSource1.DataMember = "arm";
bindingSource2.DataSource = bindingSource1;
bindingSource2.DataMember = "armUsers";
gridControl1.DataSource = bindingSource1;
gridControl2.DataSource = bindingSource2;
Please help me figure out how to hide unnecessary columns in the GridControl. Such as Id) Can the DataTable be used? If possible an example
After you set the DataSource for both grids, you can set the Visible property for the Id column to false.
Assuming the DefaultView is a GridView, the following code should do the job.
(gridControl1.DefaultView as GridView).Columns["Id"].Visible = false;
As shown in the picture below, the product I am looking for from the SearchLookupEdit object I added on Gridview
I want to add them to the related columns on Gridview.
Despite my research I have not found or used incorrect search words. I did not get it.
When SearchLookupEdit is selected, I want to insert the column named CARPAN in the Çarpan column on the Gridview.
The codes I use in the SearchLookupEdit object
DataTable Veri = new DataTable();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["STOK"].ConnectionString);
conn.Open();
SqlDataAdapter __Da = new SqlDataAdapter("select ID,AD,CARPAN from STOK_BIRIM order by ID desc", conn);
__Da.Fill(Veri);
repositoryItemSearchLookUpEdit1.DataSource = Veri;
repositoryItemSearchLookUpEdit1.DisplayMember = "AD";
repositoryItemSearchLookUpEdit1.ValueMember = "ID";
repositoryItemSearchLookUpEdit1View.Columns.Clear();
repositoryItemSearchLookUpEdit1View.Columns.AddVisible("AD", "BİRİM ADI");
repositoryItemSearchLookUpEdit1View.Columns.AddVisible("CARPAN", "CARPAN");
repositoryItemSearchLookUpEdit1View.Columns["CARPAN"].Visible = false;
conn.Close();
I have a dropdownlist that fills like:
cmd.CommandText = "select * from dbo.pelischool";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = ds.Tables[0].Columns["schoolName"].ToString();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();
Now I want to read the listing number of the selected item(in order to place it in a query).
So, while my dropdownlist has values like "Oxford University", "MIT University", "Simon Fraser University" etc, I want clicking an item from the list, store the value of this item in the list. To be more specific, if clicking the first item, then
int number = 1. if clicking the second item, then
int number = 2.
Is it possible to be done with something like:
DropDownList1.SelectedItem.????;
Thank you for your time!
You didn't bind item's value to dropdownlist, so you need to bind it.
like this
DropDownList1.DataValueField = ds.Tables[0].Columns["Column name that contain the value of Item"].ToString();
and then you can use to get it's value like as follow
string item_value = DropDownList1.SelectedValue;
If you want selected Value then use
DropDownList1.SelectedValue
and if you want selected item then use
DropDownList1.SelectedItem
In addition to Nitin Kumar answer, selected item text can be obtained by:
var selectedText = DropDownList1.SelectedItem.Text;
Selected Value
var selectedValue = DropDownList1.SelectedValue;
cmd.CommandText = "select * from dbo.pelischool";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = ds.Tables[0].Columns["schoolName"].ToString();
DropDownList1.DataValueField = ds.Tables[0].Columns["SomeValueColumn or id column"];
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();
and then you can use...
string id = DropDownList1.SelectedValue.ToString();
Try this.
cmd.CommandText = "select id,schoolName from dbo.pelischool";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt );
if (dt != null)
{
if (dt.Rows.Count > 0)
{
ddlPosition.DataSource = dt;
ddlPosition.DataTextField = "schoolName";
ddlPosition.DataValueField = "Id";
ddlPosition.DataBind();
}
}
In my database I have 4 column. I fetch this database value in a datagridview. But I want to add two columns in datagridview. So, I want to make a datagridview with 6 columns. Within this 6 columns 4 columns will filled by database value. How can I do this?
OleDbConnection con = new OleDbConnection("CONNECTION STRING");
con.Open();
DataTable dtusers = new DataTable();
OleDbCommand cmd = new OleDbCommand("Select Code,Description,Qnty,Rate from PurchaseTable'", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
dataGridView1.Columns[0].Name = "Code ";
dataGridView1.Columns[1].Name = "Description";
dataGridView1.Columns[2].Name = "Qnty";
dataGridView1.Columns[3].Name = "Rate";
con.Close();
Here are 4 columns. Code,Description,Qnty,Rate. I want to add two more columns in this datagridview. Amount and Narration. But Amount and Narration columns are not present in PurchaseTable.How can I do this?
If you want blank columns then create them and insert. If the data is in the database then add a join to your query to retrieve the data.
Adding blank columns
DataGridView dgv = new DataGridView();
dgv.DataSource = dtusers;
DataGridViewColumn amount = new DataGridViewColumn();
amount.HeaderText = "Amount";
amount.Name = "Amount";
dgv.Columns.Insert(0, amount);
DataGridViewColumn narration = new DataGridViewColumn();
narration.HeaderText = "Narration";
narration.Name = "Narration";
dgv.Columns.Insert(0, narration);
Assumed that you already have dtusers ..
Do Column adding to dtuser .. http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx
Do looping for all rows in your table to fill your new column
Assign the table as dgv datasource .. dataGridView1.DataSource = dtusers;
Because your column names are hardcoded, I think you can create a predefined columns in your datagridview through designer(Click datagridview -> choose Edit Columns).
Create all 6 columns you want. On the first four set DataPropertyName to name of the column from your query. Last two leave empty.
And remember set datagridview.AutoGeneratedColumns = false; before you binding data to datagridview...(somewhere in form_Load handler)
After this your code will be:
using (OleDbConnection con = new OleDbConnection("CONNECTION STRING"))
{
con.Open();
DataTable dtusers = new DataTable();
using(OleDbCommand cmd = new OleDbCommand("Select Code,Description,Qnty,Rate from PurchaseTable'", con))
{
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
}
con.Close();
}
I have a DataValueField and a DataTextField in my DropDownList. The data is from a DataSet and I want the dropdownlist to show the text field of my Dataset as preselected text. The Dataset is filled with data from a mysql table, containing "id" and "text". The DropDownList code is:
<asp:DropDownList runat="server" DataValueField="id"
DataTextField="text" ID="statusList" CssClass="viewItemRight"
AutoPostBack="true"></asp:DropDownList>
If there is no DataValueField-Tag the DropDownList shows correctly the text-value from my DataSet as preselected text in my DropDownList. But if I add the DataValueField the DataTextField doesnt showing any preselected Text in the DropDownList.
The code for the data is:
//load statusList
cmd = new MySqlCommand();
cmd.CommandText = "SELECT * FROM statuslist WHERE active = '1' ORDER BY sorting ASC";
cmd.Connection = con;
sda = new MySqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds);
statusList.DataSource = ds;
statusList.DataBind();
statusList.Items.Insert(0, " ");
How can I use both, the DataValueField and the DataTextField?
Try something like this
Code:
DataTable dt = populatedd();
statusList.DataSource = dt;
statusList.DataTextField = "name";
statusList.DataValueField = "id";
statusList.DataBind();
public DataTable populatedd()
{
string myQuery = "select id,name from yourtable order by name";
SqlDataAdapter dap = new SqlDataAdapter(myQuery, con);
DataSet ds = new DataSet();
dap.Fill(ds);
return ds.Tables[0];
}
Markup
<asp:DropDownList runat="server" ID="statusList" CssClass="viewItemRight"
AutoPostBack="true"></asp:DropDownList>