Devexpress Get the value from gridview child - c#

I use this code to generate the data and output it in my gridview
string sql = "Sql Query";
string sqlCredit= "Sql Query";
string sqlCreditPayment = "Sql Query";
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
ds.EnforceConstraints = false;
ds.DataSetName = "Receivables";
ds.Tables.Add((con.ShowResult(sql, ref da)).Tables[0].Copy());
ds.Tables[0].TableName = "dtReceivables";
ds.Tables.Add((con.ShowResult(sqlCredit, ref da)).Tables[0].Copy());
ds.Tables[1].TableName = "dtCredit";
ds.Tables[1].Columns[1].ColumnMapping = MappingType.Hidden;
ds.Tables[1].Columns[7].ColumnMapping = MappingType.Hidden;
ds.Tables.Add((con.ShowResult(sqlCreditPayment, ref da)).Tables[0].Copy());
ds.Tables[2].TableName = "dtCreditPayment";
ds.Tables[2].Columns[0].ColumnMapping = MappingType.Hidden;
DataRelation dr0 = new DataRelation("CreditList", ds.Tables[0].Columns["Id"], ds.Tables[1].Columns["DocSupplierId"]);
ds.Relations.Add(dr0);
DataRelation dr1 = new DataRelation("CreditPaymentList", ds.Tables[1].Columns["Id"], ds.Tables[2].Columns["SourceId"]);
ds.Relations.Add(dr1);
slipDashBoard.DataSource = ds.Tables["dtReceivables"];
slipDashBoard.ForceInitialize();
gridView1.BestFitColumns();
Guys. Pls help. i want to achieve something like this when i click on the gridview's children. thnx in advance

The main idea in this case is to obtain an instance of the GridView class which was clicked. XtraGrid creates clones of the pattern View which is created at design time and use these clones to disply data. Here is the code which should work:
GridView gridView = sender as GridView;
var value = gridView.GetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["Num"));
MessageBox.Show(value.ToString());
Since your child GridView is created automatically, there are two approaches:
1) handle the GridControl's Click event handler:
private void gridControl1_Click(object sender, EventArgs e) {
GridControl grid = sender as GridControl;
Point p = new Point(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y);
GridView gridView = grid.GetViewAt(p) as GridView;
if(gridView != null)
MessageBox.Show(gridView.GetFocusedRowCellDisplayText("Num"));
}
2) handle the GridView1 MasterRowExpanded event handler:
private void gridView1_MasterRowExpanded(object sender, CustomMasterRowEventArgs e) {
GridView master = sender as GridView;
GridView detail = master.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
detail.Click += new EventHandler(detail_Click);
}
void detail_Click(object sender, EventArgs e) {
GridView gridView = sender as GridView;
var value = gridView.GetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["Num"));
MessageBox.Show(value.ToString());
}

If you create your grid on runtime you have an instance like gridview2. Now you can add the click event with gridview2.Click += new EventHandler(gridview2_Click);
Then you will get sth. like this:
void view_Click(object sender, EventArgs e)
{
//take the code from platons post...
}

Related

How to add values from dropdownlist and checkbox to gridview

EDIT: added a code and a image reference `public partial class DodavanjeNamirnice : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dtTemp = new DataTable(); ;
dtTemp.Columns.Add(new DataColumn("Namirnica", typeof(string)));
dtTemp.Columns.Add(new DataColumn("Mjerna Jedinica", typeof(string)));
Session["Data"] = dtTemp;
}
}
protected void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["Data Source =.\\SQLEXPRESS; Initial Catalog = pra; Integrated Security = True"].ConnectionString;
string query = "SELECT * FROM Namirnica";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
var dataTableFromSession = Session["Data"] as DataTable;
var dataRow = dataTableFromSession.NewRow();
dataRow["Namirnica"] = DropDownList2.SelectedItem.Text;
dataRow["Mjerna Jedinica"] = CheckBoxList1.SelectedItem.Text;
dataTableFromSession.Rows.Add(dataRow);
Session["Data"] = dataTableFromSession;
GridView1.DataSource = dataTableFromSession;
GridView1.DataBind();
}
}`I got 2 dropdownlists , first one is filtering data in the 2nd,also 1st dropdownlist is connected to sql table as is the other one.
I have a checkbox which displays data from another table.
And my problem is: I want to add values I selected from 2nd dropdownlist and the checkboxlist to gridview in my webform.
I tried adding new columns manually but that ends up with displaying only first value from the dropdownlist, also displays only one value from the checkboxlist.
https://gyazo.com/59ea939b26deb55d3f31e68057249253
Set up a method to change the selected or created cell into a DataGridViewComboBoxCell and then feed it the datasource of your drop down list.
//could be whatever event you want such as the creation of a new DataRow in your DataGridView
private void gridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1[e.ColumnIndex, e.RowIndex] = new DataGridViewComboBoxCell();
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
cb.DataSource = dataSource;
}
You can do a similar thing for the checkbox but new DataGridViewCheckBoxCell() instead. After the user has selected a value you can just switch it back to being a regular cell if wanted.

Datagridview and two combobox (related)

i have one Datagridview and two combobox (related) When I click on dataGridView1_CellMouseClick cmbSehir.Text changes as I want. but cmbilce.Text does not change as I want!. Where do i make mistakes. I hope I made myself clear. Thanks for helping.
private void frmMusteriEkle_Load(object sender, EventArgs e)
{
GetSehir();
GetDatagridview();
}
private void GetSehir() {
db.connect();
db.SqlQuery("select * from iller");
DataTable dt = db.GeTDataTable();
DataRow dr = dt.NewRow();
dr["id"] = 0;
dr["sehir"] = "Seçiniz:";
dt.Rows.InsertAt(dr, 0);
cmbSehir.DataSource = dt;
cmbSehir.ValueMember = "id";
cmbSehir.DisplayMember = "sehir";
db.disconnect();
}
private void cmbSehir_SelectionChangeCommitted(object sender, EventArgs e)
{
if (cmbSehir.SelectedIndex != 0)
{
db.connect();
db.SqlQuery("select * from ilceler where il_id = ?");
db.command.Parameters.AddWithValue("#p", cmbSehir.SelectedValue);
DataTable dt = db.GeTDataTable();
cmbilce.DataSource = dt;
cmbilce.ValueMember = "id";
cmbilce.DisplayMember = "ilceler";
db.disconnect();
}
else
{
cmbilce.DataSource = null;
}
}
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
cmbSehir.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
cmbilce.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
}
Without seeing what GetDatagridview method does, I will assume from the lines…
cmbSehir.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
cmbilce.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
that these combo boxes are related to the data contained in columns 4 and 5. If the combo boxes have the same data source as the DataGridView then it is possible to bind all three controls to the same DataSource. After binding all three components to the same data source, clicking on a cell in the grid will automatically change the combo boxes and vice versa… changing a combo box will change the selection in the grid. There is no need to wire up the CellMouseClick or SelectionChanged events to keep the controls in sync.
Another consideration is the cmbSehir_SelectionChangeCommitted method that fires every time the user changes the selection in the cmbSehir combo box. In this method, the code queries the data base and sets the combo boxes data source. This is fine, however, are you sure you want to query the database and refill the combo box EVERY time the user changes this selection? I am guessing one query would be sufficient, until new data is added, removed or changed.
The code below sets the DataSource for the combo boxes to the same data source of the DataGridView using different ValueMembers.
DataTable AllData;
private void Form1_Load(object sender, EventArgs e) {
AllData = GetDT();
dataGridView1.DataSource = AllData;
comboBox1.DataSource = AllData;
comboBox2.DataSource = AllData;
comboBox1.ValueMember = "sehir";
comboBox2.ValueMember = "ilceler";
}

columns: Datagridview to Datatable

I am facing this issue, I have datagridview and a datatable.
VPfn_CreateDataGrid();//This fuction creates gridview columns
DataTable invoice_table = (DataTable)invoice_data.DataSource;
now First thing, datagridview is empty when form loads. What I am trying to do is adding data to datagridview via multiple textboxes and combomoxes and for that I am using datatable.
private void btn_add_Click(object sender, EventArgs e)
{
DataRow x = invoice_table.NewRow();
x["serial_number"] = tsr.Text.ToString();
x["item"] = combo_items.SelectedItem.ToString();
x["item_rate"] = tr.Text;
x["item_qty"] = tq.Text;
x["item_unit"] = combo_unit.SelectedItem.ToString();
x["item_vat"] = combo_vat.SelectedItem.ToString();
x["amount"] = ta.Text;
invoice_table.Rows.Add(x);
invoice_data.Refresh();
}
And the error is "Column 'serial_number' does not belong to table"
first you have to create a the data table with the specific column. while your datagridview is empty the DataTable also will be empty.
DataTable invoice_table = (DataTable)invoice_data.DataSource;
infront of this..
while loading your form you can create datatable with the columns.
DataTable invoice_table; //Global
private void load()
{
invoice_table = new DataTable();
invoice_table.Columns.Add("serial_number", typeof(int));
invoice_table.Columns.Add("item");
.....
}
after that
private void btn_add_Click(object sender, EventArgs e)
{
DataRow x = invoice_table.NewRow();
x["serial_number"] = tsr.Text.ToString();
x["item"] = combo_items.SelectedItem.ToString();
x["item_rate"] = tr.Text;
x["item_qty"] = tq.Text;
x["item_unit"] = combo_unit.SelectedItem.ToString();
x["item_vat"] = combo_vat.SelectedItem.ToString();
x["amount"] = ta.Text;
invoice_table.Rows.Add(x);
invoice_data.Refresh();
}
Try this...

Show datatable in tabcontrol

i have a datagridview . and tabcontrol. before i use this code to load datatable in newform with value selected. here is the code
private void dgvSatuan_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dgvSatuan.ReadOnly = false;
str = dgvSatuan.Rows[e.RowIndex].Cells[0].Value.ToString();
i = dgvSatuan.Rows[e.RowIndex].Index;
fSatuan opSatuan = new fSatuan(str, i, ID);
opSatuan.ShowDialog();
showDataSatuan();
}
how to load datatable when datagridview is selected and the value detail is show to textView in tabPage1?

how to bind the radcombobox at runtime in asp.net

I have a webpage that has a Telerik RadComboBox in radgrid control on the page,and i have a SqlserverCe database.My issue is how can i write the code to bind the RadCombobox at page load event in asp.net please help me.....
Items are bound to RadComboBox basically in the same way as to an ASP.NET DropDownList.
You can bind the RadComboBox to ASP.NET 2.0 datasources, ADO.NET DataSet/DataTable/DataView, to Arrays and ArrayLists, or to an IEnumerable of objects. And of course you can add the items one by one yourself. With RadComboBox, you'll use RadComboBoxItems instead of ListItems.
In one way or other, you'll have to tell the combobox what is each item's text and value.
Working with Items in Server Side Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadComboBoxItem item1 = new RadComboBoxItem();
item1.Text = "Item1";
item1.Value = "1";
RadComboBox1.Items.Add(item1);
RadComboBoxItem item2 = new RadComboBoxItem();
item2.Text = "Item2";
item2.Value = "2";
RadComboBox1.Items.Add(item2);
RadComboBoxItem item3 = new RadComboBoxItem();
item3.Text = "Item3";
item3.Value = "3";
RadComboBox1.Items.Add(item3);
}
}
Binding to DataTable, DataSet, or DataView:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=LOCAL;Initial Catalog=Combo;Integrated Security=True");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Text], [Value] FROM [Links]", con);
DataTable links = new DataTable();
adapter.Fill(links);
combo.DataTextField = "Text";
combo.DataValueField = "Value";
combo.DataSource = links;
combo.DataBind();
}
}
EDIT: RadComboBox in a Grid:
Inside a RadGrid, it is perhaps easiest to use load on demand, by setting EnableLoadOnDemand="True" and handling the OnItemsRequested event.
protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
string sql = "SELECT [SupplierID], [CompanyName], [ContactName], [City] FROM [Suppliers] WHERE CompanyName LIKE #CompanyName + '%'";
SqlDataAdapter adapter = new SqlDataAdapter(sql,
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
adapter.SelectCommand.Parameters.AddWithValue("#CompanyName", e.Text);
DataTable dt = new DataTable();
adapter.Fill(dt);
RadComboBox comboBox = (RadComboBox)sender;
// Clear the default Item that has been re-created from ViewState at this point.
comboBox.Items.Clear();
foreach (DataRow row in dt.Rows)
{
RadComboBoxItem item = new RadComboBoxItem();
item.Text = row["CompanyName"].ToString();
item.Value = row["SupplierID"].ToString();
item.Attributes.Add("ContactName", row["ContactName"].ToString());
comboBox.Items.Add(item);
item.DataBind();
}
}
You can also bind the combobox manually in the grid's OnItemDataBoundHandler event:
protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode)
{
GridEditableItem item = (GridEditableItem)e.Item;
if (!(e.Item is IGridInsertItem))
{
RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
// create and add items here
RadComboBoxItem item = new RadComboBoxItem("text","value");
combo.Items.Add(item);
}
}
}

Categories