C# Combobox ID in datagridview query - c#

hi I'm Litte big english I want ComboboxID in datagridview query..
Aşağıdaki kod için yardıma ihtiyacım var ... Sanırmı çok kolay bir
şeydir ama yeni başladığım için içinden çıkamıyorum
public void cmbComp_SelectedValueChanged(object sender, EventArgs e)
{
ServisHEntities Context = new ServisHEntities();
cmbCus.DataSource = Context.SCustomers.ToList();
cmbCus.DisplayMember = "SDescription";
cmbCus.ValueMember = "SCustomerID";
cmbCus.SelectedIndex = -1;
cmbCus.Invalidate();
}
public void cmbCus_SelectedValueMemberChanged(object sender, EventArgs e)
{
{
grdUser.DataSource = (from USR in oe.SUsers
where (USR.SCustomerID == /* cmbCus.ValueMember() I Want SCustomerID is here /// ID nin buraya gelmesini istiyorum */ && USR.SUserStatus == true && USR.SUserType == 2)
select new { USR.SUserName, USR.SUserSurname, USR.SPhone, USR.SSEmail }).ToList();
}
}

This should give you the selected item's value. Good luck! You can use cmbCus.SelectedValue inline like you have in your example as well.
var val = cmbCus.SelectedValue;

Related

How can I get the value of the data contained in the Asp Dropdown?

There are 4 values in my dropdown list, and if there is an "x" value in it, I want to hide that value. How can I do this using the If command.
protected void dr_alans_DataBound(object sender, EventArgs e)
{
if (dd_mainAlan.SelectedItem.Text == "Yönetici")
{
if (dr_alans.Items.FindByValue("22").Value=="22")
{
dr_alans.Items.FindByValue("22").Enabled = false;
}
else if (dr_alans.Items.FindByValue("23").Value == "23")
{
dr_alans.Items.FindByValue("23").Enabled = false;
}
}
}
If it is ASP.NET Forms, you should be able to do it like this maybe. This should hide the items with an x in the Value.
foreach (var itm in dd_mainAlan.Items)
{
var it = (ListItem)itm;
it.Enabled = !it.Value.Contains("x");
}

How to Load all the Objects in an Array List

I am trying to figure out how to load through an Array List of Objects. I am able to retrieve the last Object in the Array, but it will not let me Load any other Object after that. here is part of the code I have. As you can see it saves the object to the List, but when I click the loadLastBtn it will only load the most recent entry and if I hit it again after that nothing loads.
List<Members> lstMembers = new List<Members>();
private int hold;
private void submitBtn_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(firstNameTxt.Text) || string.IsNullOrEmpty(lastNameTxt.Text)
|| string.IsNullOrEmpty(userNameTxt.Text) ||
string.IsNullOrEmpty(passwordTxt.Text) || string.IsNullOrEmpty(confPassTxt.Text)
|| string.IsNullOrEmpty(majorBox.Text) || string.IsNullOrEmpty(specialtyBox.Text))
{
MessageBox.Show("You must enter in all fields before moving forward");
}
else
{
Members m1 = new Members(firstNameTxt.Text, lastNameTxt.Text, userNameTxt.Text,
passwordTxt.Text, confPassTxt.Text, majorBox.Text,
specialtyBox.Text);
lstMembers.Add(m1);
}
}
private void loadLastBtn_Click(object sender, EventArgs e)
{
firstNameTxt.Text = lstMembers[hold].FirstName;
lastNameTxt.Text = lstMembers[hold].LastName;
userNameTxt.Text = lstMembers[hold].UserName;
passwordTxt.Text = lstMembers[hold].Password;
confPassTxt.Text = lstMembers[hold].ConfPassword;
majorBox.Text = lstMembers[hold].Major;
specialtyBox.Text = lstMembers[hold].Specialty;
hold++;
}
I have edited my answer. this will now print each user one by one each time I hit loadLastBtn, but it does show them from the first user to the last, where I need it to shower the last user to the first.
What you need is something like this:
// -1 Indicates that you should start at the end of the list
int index = -1;
private void loadButton_Click(object sender, EventArgs e)
{
if (members != null && members.Count > 0) // Avoid accessing if list is empty or null
{
if (index == -1)
index = members.Count - 1;
firstNameTxt.Text = lstMembers[index].FirstName;
lastNameTxt.Text = lstMembers[index].LastName;
userNameTxt.Text = lstMembers[index].UserName;
passwordTxt.Text = lstMembers[index].Password;
confPassTxt.Text = lstMembers[index].ConfPassword;
majorBox.Text = lstMembers[index].Major;
specialtyBox.Text = lstMembers[index].Specialty;
if (index == 0) // Reached beginning of array
index = -1; // Indicate that next time the last element must be accessed
else
--index;
}
}
private int hold = lstMembers.Count -1;
private void loadLastBtn_Click(object sender, EventArgs e)
{
firstNameTxt.Text = lstMembers[hold].FirstName;
lastNameTxt.Text = lstMembers[hold].LastName;
userNameTxt.Text = lstMembers[hold].UserName;
passwordTxt.Text = lstMembers[hold].Password;
confPassTxt.Text = lstMembers[hold].ConfPassword;
majorBox.Text = lstMembers[hold].Major;
specialtyBox.Text = lstMembers[hold].Specialty;
hold--;
}

How To Get Values From Cells In Radgrid C# asp.net

I am trying to get specific cell value so i can pass it in my method when i press the button but i get always null on both (and i know that is not null).
P.s: None row is selected because i made a loop to get all the rows. The variable "p" is getting correct the number of the rows that i have on the grid.
protected void PostRadButton_Click(object sender, EventArgs e)
{
int p;
if (DocStatTxtBox.Text == "2")
{
foreach (GridDataItem item in RadGrid1.Items)
{
p = item.RowIndex;
string itemcodeparam = item["ItemCode"].Text;//error null (4th cell)
int quantityparam = Convert.ToInt16(item.Cells[5].Text);//error null
Boolean x = Methods.UpdateStock(WhTxtBoxRadDropDownList.SelectedValue,itemcodeparam,-quantityparam);
}
}
}
Finally i did it with this code
protected void PostRadButton_Click(object sender, EventArgs e)
{
int p;
if (DocStatTxtBox.Text == "2")
{
foreach (GridDataItem item in RadGrid1.Items)
{
p = item.RowIndex;
Label itemparam = (Label)item["ItemCode"].FindControl("ItemCodeLabel");
Label qparam = (Label)item["Quantity"].FindControl("QuantityLabel");
string itemcodeparam = itemparam.Text;
int quantityparam = Convert.ToInt16(qparam.Text);
Boolean x = Methods.UpdateStock(WhTxtBoxRadDropDownList.SelectedValue, itemcodeparam, -quantityparam);
}
}
}

How to get the textbox value in ascx page?

In my project I have 3 user controls, those are displayed in master page. When I entered the value in textbox it doesn't getting current value, it will display previous value.
Code:
1st Data Bound:
protected void dlFirstZone_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) && (e.Item.DataItem != null))
{
using (GroceriesServiceClient groceriesServiceClient = new GroceriesServiceClient())
{
HomeMaster pp = (HomeMaster)e.Item.DataItem;
int prdItem = pp.ProductId;
ShoppingCart shopingCartParameter = new ShoppingCart();
//if (pp.DisplayOrder == 1)
//{
Products products = new Products();
if (basePage.BasePageWebSession != null)
{
shopingCartParameter.UserId = Convert.ToInt32(basePage.BasePageWebSession.UserId);
cartlist = groceriesServiceClient.ShoppingCart_UserProductsList(shopingCartParameter);
var td = (from c in cartlist
where c.ProductId == prdItem
select c);
if (td.ToList().Count > 0)
{
TextBox txtQtyDataview = (TextBox)e.Item.FindControl("txtQty");
txtQtyDataview.Text = td.First().Quantity.ToString();
}
}
//}
//else
}
}
}
2nd ItemCommand event handler:
protected void dlProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
HomeMaster q = (HomeMaster)e.Item.DataItem;
if (e.CommandName == "AddToCart")
{
using (GroceriesServiceClient gsc = new GroceriesServiceClient())
{
ShoppingCart shoppingcart = new ShoppingCart();
shoppingcart.UserId = basePage.BasePageWebSession.UserId;
shoppingcart.UserName = basePage.BasePageWebSession.UserName;
shoppingcart.ProductId = Convert.ToInt32(Convert.ToInt32(dlProductDataView.DataKeys[e.Item.ItemIndex]));
TextBox tb1 = (TextBox)e.Item.FindControl("txtQty");
if (!string.IsNullOrEmpty(tb1.Text))
shoppingcart.Quantity = Convert.ToInt32(tb1.Text);
else
shoppingcart.Quantity = 1;
shoppingcart = gsc.ShoppingCart_InsertOrUpdate(shoppingcart);
Response.Redirect(Request.RawUrl);
}
}
}
You can use findControl to find the textbox within a user control, like this (assuming you have a textbox called tb1 in a userconrol called uc1:
me.uc1.findControl("tb1").text
The better way, though, is to expose a public function that can be called to return the value:
In UserControl, create a public function:
public function getValue() as string
return me.tb1.text
end Function
Then you can access it from any page or control that has a reference to the user control:
dim whatsMyName as string = me.uc1.getValue()

Searching a DataGridView for a match or partial match

Tried to sift through the google results; no luck. What I'm trying to do is, given some text in a TextBox: search for at least a partial match in any column of my DataGridView, and have the control select the first row (show it as the top row and have it highlighted) that it encounters with that partial match.
Here is my declaration of the DataSource for the DataGridView and how the columns are organized:
var queryData = from va in xdoc.Descendants("language")
select new
{
StringID = va.Parent.Parent.Attribute("id").Value,
Language = va.Attribute("name").Value,
LanguageData = va.Element("value").Value,
};
var organizedData = from x in queryData
group x by x.StringID into xg
select new
{
StringID = xg.Key,
English = xg.SingleOrDefault(x => x.Language == "ENGLISH_US").LanguageData,
Custom = xg.SingleOrDefault(x => x.Language == languageBox.SelectedItem.ToString()).LanguageData,
};
mainView.DataSource = organizedData.ToList();
And here is the current definition of the function that handles a click of the 'Search' button:
private void searchButton_Click(object sender, EventArgs e)
{
int currentIndex = mainView.CurrentRow.Index;
if (searchBox.Text.Length == 0)
{
mainView.CurrentCell = mainView[0,0];
mainView.Focus();
return;
}
}
Something like this might get you close:
string searchForText = "whatever";
DataGridViewRow rowFound = mainView.Rows.OfType<DataGridViewRow>()
.FirstOrDefault(row => row.Cells.OfType<DataGridViewCell>()
.Any(cell => ((dynamic)cell.Value).StringID.Contains(searchForText)));
if (rowFound != null) {
mainView.Rows[rowFound.Index].Selected = true;
mainView.FirstDisplayedScrollingRowIndex = rowFound.Index;
}

Categories