I have a User Control with an Ultragrid. In a particular form where I add a ValueList. The value list will not show for the particular column I'm interested in. If I then code in another column instead by changing the index value for columns I get the value list in the column.
The code looks like:
private void AddCombo(object sender, UcUltraGen.RowClickArgs e)
{
ValueList vl;
if (!ucUltraGridMain.Grid.DisplayLayout.ValueLists.Exists("Texas"))
{
vl = ucUltraGridMain.Grid.DisplayLayout.ValueLists.Add("Texas");
}
else
{
vl = ucUltraGridMain.Grid.DisplayLayout.ValueLists["Texas"];
}
var row = e.VariantRow;
List<PcBase> list = PcBase.PcBaseList.Where(x => x.VariantId == row.Cells["Id"].Text).ToList();
AddValueList(list, vl);
ucUltraGridMain.Grid.DisplayLayout.Bands[0].Columns[1].ValueList =
ucUltraGridMain.Grid.DisplayLayout.ValueLists["Texas"];
And if I change to
...
ucUltraGridMain.Grid.DisplayLayout.Bands[0].Columns[2]
It works. How can I have changed the behavior of columns[1]?
The property in column[1] was read only that is it only had a "get" implemented. By adding a set it worked. Hope this helps someone.
Related
When I clicked on a cell in my DataGridView, the values are passed on my Textboxes, but I have a problem when it comes to my combobox, it just stays null. I've already tried SelectedItem and SelectedIndex but it stays null. I've manage to place the value in my combobox using SelectedText but once I've updated my database, I'm getting a NullReferenceException in my combobox, here's my code:
private void dgvStudentRecord_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dgvStudentRecord.Rows[e.RowIndex];
txtStudNum.Text = row.Cells["studentId"].Value.ToString();
txtStudName.Text = row.Cells["studentName"].Value.ToString();
cboSection.SelectedText = row.Cells["section"].Value.ToString();
numPrelim.Value = Convert.ToDecimal(row.Cells["prelim"].Value);
numMidterm.Value = Convert.ToDecimal(row.Cells["midterm"].Value);
numFinals.Value = Convert.ToDecimal(row.Cells["finals"].Value);
}
}
You're going to have some headache doing it the way you are approaching it because the ComboBox does not handle unexpected values well at all, and the SelectText property it not doing what you think its doing (its NOT selecting an item from its internal list when you set that property) (see: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext(v=vs.110).aspx)
You are going to be better of writing something like:
int index = cboSection.FindString(row.Cells["section"].Value.ToString());
if(index > -1)
{
cboSection.SelectedIndex = index;
}
else
{
object newSection = row.Cells["section"].Value.ToString();
cboSection.Items.Add(newSection);
cboSection.SelectedItem = newSection;
}
Edited to show conditional select or add.
Final edit... Doh.
Try this, ComboBox.Text property
combobox1.Text=row.Cells[cellIndex].Value.ToString();
I have a datagridview that contains list of subjects populated from Subject table from database.Columns include
Select(checkbox),
SubjectId,
SubjectName,
SubjectGroup.
Now I want if a user Selects on any of the desired rows, the corresponding SubjectId's should be added to a List. I have made and inserted into the desired table in the database.
The problem is that the new column of checkboxes I have added to this datagridview is not being detected.
My code is:
foreach (DataGridViewRow row in gvSubjectsOpted.Rows)
{
if (Convert.ToBoolean(gvSubjectsOpted.SelectedRows[0].Cells["SelectId"].Value=true))
{
olist.Add(gvSubjectsOpted.SelectedRows[0].Cells["SubjectId"].Value.ToString());
}
}
Late to the party. I had the same issue with trying to get the checkbox column by name, use the index instead. Here is a linq example assuming the checkbox is column 0 and the stored values for TrueValue and FalseVale are true and false respectively.
var checkedRows = from DataGridViewRow r in gvSubjectsOpted.Rows
where Convert.ToBoolean(r.Cells[0].Value) == true
select r;
foreach (var row in checkedRows)
{
olist.Add(row.Cells["SubjectId"].Value.ToString());
}
I realise this is an old post but I came across it and didn't think it was really answered in an efficient way so I thought I would add my method.
I have a similar block in my windows app. I read the values from the grid when the user clicks a button, and I want to know which rows they checked. As the checkboxes are in Cell 0 and the data I want is in Cell 1, I use the following code. Note the cast: it is important as it allows us the use the Where clause and therefore just a single line of code to get the collection of data. I could use the name of the cells instead of magic index numbers but then it would not fit your app so I put numbers instead (you should use names)
var checkedRows = dataGridView
.Rows
.Cast<DataGridViewRow>()
.Where(x => x.Cells[0].Value.ToString() == "1")
.Select(x => x.Cells[1]);
Note that this will give you an IEnumerable of type DataGridViewCell. If you want you can either add something like .Value.ToString() to the select or do this when you use your collection.
You question is similar to another SO question.
Check the answer of this Datagridview checkboxcolumn value and functionality.
Try this
foreach(GridViewRow r in gvSubjectsOpted.Rows)
{
GridViewCheckBoxColumn c = r.cells[0].Controls[0] as GridViewCheckBoxColumn;
if(c.Checked)
{
//Do something.
}
}
private void button1_Click(object sender, EventArgs e)
{
string subjId;
List<string> lines = new List<string>();
for (int i = 0; i < gvSubjectsList.Rows.Count; i++)
{
bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);
if (Ischecked == true)
{
subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();
lines.Add(subjId);
}
}
comboBox1.DataSource = lines;
}
//the most important thing is to set 'true' and 'false' values against newly added checkboxcolumn instead of '0' and '1'...that is,
CBColumn.FalseValue = "false";
CBColumn.TrueValue = "true";
I use a DevexpressGridView to display all TOPIC (id,title,content)
<dx:ASPxGridView ID="gv" runat="server"
OnSelectionChanged="gv_SelectionChanged" >
I have grid_SelectionChanged event:
protected void gv_SelectionChanged(object sender, EventArgs e)
{
int id= selected row...???; //how can I get the value of selected row
string sql = "select * from TOPIC where idTOPIC="+id;
DataTable topic = l.EXECUTEQUERYSQL(sql);
TextBox1.Text = topic.Rows[0][1].ToString();
}
...
It seems gv.SelectedRow method isn't exist in DevGridview.
As recommended, I've tried with FocusedRowIndex method, but I really dont know the right syntax to get the value of selected row.
Help!!!
Changing the selection is different from changing the focused row. See the documentation for Selection for the difference between the two.
You can use gv.GetSelectedFieldValues to get the rows which are selected.
var ids = gv.GetSelectedFieldValues("id");
foreach( var id in ids )
DoSomethingWithObject(id);
You should handle the FocusedRowChanged event if you're interested in the focused row.
You can use the FocusedRowIndex value to index the rows of gv.DataSource, for example:
DataTable ds = (DataTable)gv.DataSource;
var id = ds.Rows[gv.FocusedRowIndex]["id"];
or you can use var id = gv.GetRowValues(gv.FocusedRowIndex, "id").
I've found my answere here after a long time searching google:
http://www.devexpress.com/Support/Center/Question/Details/Q347704
Use the ASPxGridView.GetSelectedFieldValues method get selected row values on the server side.
You can also get selected data row as
int rowHandle = gridView1.FocusedRowHandle;
if (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle)
{
return this.gridView1.GetDataRow(rowHandle);
}
This would return DataRow
Please note this is when I am using Devexpress gridControl in WinForms
If you want to get only ID field value you can use this
int id = Convert.ToInt32(gv.GetRowValues(gv.FocusedRowIndex, "ID").ToString());
if you have an object you can use this
Personels selectedPersonel = gv.GetRow(gv.FocusedRowIndex) as Personels;
and get value method is
int ID = selectedPersonel.ID;
In Winforms I wanted to show a list box having item index number and item exactly as shown in the below . I have gone through many SO links and also many articles also, most of them are using ListView instead of ListBox and also instead of showing List Item index they showing Image+ Item in it. I am ok with the listview but my objective is to show the Items with their corresponding index and on select it should give me the item only. Can anybody provide me any link or idea to achieve this in Winform using C#.
Thanks in advance.
I think best option for you is using DataGridView with two columns:
DataGridViewButtonColumn, AutoSizeMode = AllCells
DataGridViewTextBoxColumn, ReadOnly = true, AutoSizeMode = Fill
Also RowHeadersVisible and ColumnHeadersVisible to false. Result:
Code:
string[] members = { "Beverages", "Condiments", "Confections" };
dataGridView1.DataSource = members.Select((x, i) => new { Value = x, Index = i })
.ToList();
Also you need to set columns DataPropertyName to Index and Value.
UPDATE (for .NET 2.0):
string[] members = { "Beverages", "Condiments", "Confections" };
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = members;
Subscribe to CellFormatting event:
void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Value = e.RowIndex;
return;
}
e.Value = dataGridView1.Rows[e.RowIndex].DataBoundItem;
}
You can customize your Listbox
Following link will guide you right direction.
http://www.codeproject.com/Articles/15464/Extending-the-ListBox-to-show-more-complex-items
I think you should consider setting the DrawMode property on the listbox to DrawMode.Manual and handle the drawing of the items in your code.
Have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.drawmode.aspx
(Asp.net/C# VS2008)
I have a datagrid populated by a database and when pressing Edit, a form opens and populates fields/controls from that datagrid line.My issue lies when trying to populate a dropdownlist box, which has a datasource from an ENum List.I Just cannot get the text from the datagrid cell to show up in the ddL, it should also equal one of the Enum Items and auto select it.
My Code
Pull from the datagrid cell gives me “Low”
ddl_reg.Text = e.Item.Cells[25].Text;
public void Populate_regstatus_dropdownlist()
{
//if (!IsPostBack)
//{
// ddl_reg.DataSource = Enum.GetNames(typeof(regstatus));
// ddl_reg.DataBind();
//}
//if (!IsPostBack)
//{
// foreach (int value in Enum.GetValues(typeof(regstatus)))
// {
// ddl_reg.Items.Add(new ListItem(Enum.GetName(typeof(regstatus), value), value.ToString()));
// }
//}
ddl_reg.DataSource = Enum.GetNames(typeof(regstatus ));
//ddl_reg.DataValueField = regstatus;
//ddl_reg.DataTextField = "Low";
//ddl_reg .SelectedItem = Enum.GetName(typeof (regstatus ));
ddl_reg.DataBind();
//ddl_reg.SelectedIndex = ddl_reg.Items.IndexOf(ddl_reg.Items.FindByText("Low"));
}
public enum regstatus
{
NotSelected,
Low,
Medium,
High
}
Error received is;
ddl_reg' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I am new to C# but through searching your site i realise this means the value is not seen or has not been pulled and would really apreciate some help, or pointing in the right direction, cheers.
I believe the problem is that the drop down list doesn't contain the item you are passing. You may check it before changing the selected value. Other chances are that the value you are getting from e.Item.Cells[25].Text may contain spaces, so you may trim before setting it to the drop down.
if (ddl_reg.Items.FindByText("Low") != null)
{
ddl_reg.Text = e.Item.Cells[25].Text;
}
else
{
//Not found
}