Visibility of buttons in gridview when selecting a row ASP.net c# - c#

Is it possible to make the edit button on the right side of my gridview table visible when selecting a row and when I select another row the previous row's edit button will disappear. Can you please help me coz I really need it to be done. Thanks!
protected void TraineeGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
for (int i = 0; i < TraineeGrid.Rows.Count; i++)
{
if (TraineeGrid.SelectedIndex == i)
{
TraineeGrid.Rows[i].Cells[1].Visible = true;
}
}
}

Related

Fill Textbox with Data from RadGrid

When I click on an item I would like to fill my TextBox with numbers from a column from Grid2 after Grid1 is clicked. Right now if I click on an item in Grid1 it will then run a procedure that will fill Grid2 with data. Here is an example of the current functionality with picture attached, I click an item from Grid1 and it has 3 results, my textbox will still display as an empty textbox. Then I click another item in Grid1 and now my textbox will display the 3 results from the previously clicked item. How can I display the correct numbers in my textbox after an item is selected in Grid1.
I have tried a few different methods including:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
divDetails.Visible = true;
RadTextBox1.Text = "";
for (int i = 0; i < Grid_Product_List_Details.Items.Count; i++)
{
RadTextBox1.Text += Grid_Product_List_Details.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
}
and:
protected void Grid_Product_List_Details_PreRender(object sender, EventArgs e)
{
RadGrid grid = (RadGrid)sender;
if (grid.Items.Count > 0)
{
RadTextBox1.Text = "";
}
for (int i = 0; i < grid.Items.Count; i++)
{
RadTextBox1.Text += grid.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
RadTextBox1.DataBind();
}
and this:
protected void Grid_Product_List_Header_SelectedIndexChanged(object sender, EventArgs e)
{
RadTextBox1.Text = "";
for (int i = 0; i < Grid_Product_List_Details.Items.Count; i++)
{
RadTextBox1.Text += Grid_Product_List_Details.Items[i].GetDataKeyValue("number").ToString() + "\n";
}
}
But those aren't working. Any suggestions?
Try to change your code a little bit.
RadTextBox1.Text = "";
foreach (GridDataItem dataItem in Grid_Product_List_Details.Items)
{
RadTextBox1.Text += dataItem.GetDataKeyValue("number").ToString() + "\n";
}
RadTextBox1.DataBind();
This should work though I suspect your earlier code should work too.
I ended up creating a procedure which fills the textbox after an item is clicked so the most recent data appears.
Try the ItemDataBound event of the second grid. You will have access to all of its data so you can put that in the RadTextBox. I suspect you provide a data source to the second grid in the SelectedIndexChanged of the first, so its Rebind() method should be called and then you will get the ItemDataBound fired for each of its rows.
Also, if you are using AJAX - disable it. It is possible that something does not get properly updated with the partial rendering. If things work fine with full postbacks, examine the responses and see why the textbox is not included in the first request so you can know how to tweak your AJAX setup.

Textboxes in an Array, Connect 4

I'm trying to create a Connect 4 game using textboxes and buttons.
If you click on the button, the textbox background color is filled in and the text will either be filled with x or y.
private void button2_Click(object sender, EventArgs e)
{
Output_textBox.AppendText("You have inserted in Column 2");
Output_textBox.AppendText(Environment.NewLine);
TextBox[] boxes = { textBox21, textBox22, textBox23, textBox24, textBox25, textBox26 };
for (int i = 0; i < 6; i++)
{
if (boxes[i].BackColor != SystemColors.HotTrack)
{
boxes[i].BackColor = SystemColors.HotTrack;
boxes[i].Text = "Y";
}
}
}
(edited on 11/6/2013 2:25pm)
The Code above doesn't seem to work even though it seems to make perfect sense to me that it should.
What i want is for the textbox to turn one by one. So if i click on the button once. textbox11 changes first. Then if textbox11 is filled, textbox12 is filled next when i click on the button again etc.
The coloring needs to be from bottom to top in a column
About me: I'm new to coding. Sorry for the trouble
Thank you in advanced

Gridview row clickable except for first column?

I'm using the following code to make the entire row of my gridview clickable:
protected void gridMSDS_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.backgroundColor='#EEFF00'";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor='White'";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);
}
}
Which works great, except now I want to add edit ability to the grid. This works, but when I have both the row clickable and editing functions turned on, clicking the "Edit" link button often fires the row click event and vice versa.
So, how can I keep row clickable, except for specified columns?
UPDATE:
Here's what I'm using.
Based on Justin's solution:
List<int> notClickable = new List<int>();
{
notClickable.Add(0);
notClickable.Add(2);
}
for(int i = 0; i < e.Row.Cells.Count; i++)
{
if (!notClickable.Contains(i))
{
e.Row.Cells[i].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);
}
}
The trick is the register the click on the specific columns that need to be clickable. The code below assumes you know the indexes that should be clickable (in this case 0).
e.Row.Cells[0].Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.gridMSDS, "Select$" + e.Row.RowIndex);

C# dynamically taking data from DataGridView

I'm trying to get my current program to take information from a dynamically created DataGridView. I have managed to get the information into the grid, and perform the required search, however now I'm really stuck.
I have added a column to the datagridview which holds a button within each row. What I'd like to do is take the value of the data from column index 1 which is in the same row as the button clicked. Confusing? Anyway, here's the code:
public void GetValues(...)
{
//Details regarding connection, querying and inserting table
.
.
.
DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
buttonCol.Name = "ButtonColumn";
buttonCol.HeaderText = "Select";
buttonCol.Text = "Edit";
//NB: the text won't show up on the button. Any help there either?
dataGridView1.Columns.Add(buttonCol);
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewButtonCell button = (row.Cells["ButtonColumn"] as DataGridViewButtonCell);
}
dataGridView1.Columns["ButtonColumn"].DisplayIndex = 0;
}
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//Here is where I'm having the trouble. What do I put in here???
}
Thanks for any help you can give!
David.
Your DataGridViewCellEventArgs contains very useful information such as RowIndex.
So something like (I don't know what you want to do with the value):
String dataYouWant = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
`
if (e.ColumnIndex != button_column_number) //column number of the button.
return;
dataGridView1.EndEdit();
bool val;
if ((dataGridView1.Rows[e.RowIndex].Cells[1].Value) != null) // column index 1...as that's what you want.
{
//d stuff you want here.
}
else
{
}
`

Selected row does't update in DataGridView

I have a datatable bound to my datagridview. One of the columns is a DataGridViewCheckBoxColumn.
By clicking on a button you should change all checkboxes in the column to true.
private void btnPublishAll_Click(object sender, EventArgs e)
{
for (int j = 0; j < this.dgrView.RowCount; j++)
{
this.dgrView[7, j].Value = true;
}
this.dgrView.EndEdit();
}
When I press the button everything seems ok(all checkboxes are true), but when I press update everything is updated except the row that was selected during btnPublishAll_Click.
What am I doing wrong?
I found the problem!
I needed to add
this.BindingContext[this.dgrView.DataSource].EndCurrentEdit();
instead of
this.dgrView.EndEdit();

Categories